Android 简单实现PopWindow 几行代码搞定

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/bbtianshi/article/details/81912554

第一步 是对应的主页面的布局

我这里面就一个textview

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".popwinds.PopActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="立即支付"
        android:textSize="30dp"
        android:background="#f00"
        android:layout_centerInParent="true"
        android:id="@+id/tv"
        />
</LinearLayout>

下面是对应popwindow的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/pop_computer"
        android:layout_margin="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:text="创建订单"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="#fff"
        />
    <TextView
        android:layout_margin="20dp"
        android:id="@+id/pop_financial"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:text="立即支付"/>
</LinearLayout>

下面就开始撸代码。对应的主页面

public class PopActivity extends AppCompatActivity implements View.OnClickListener{

    private TextView tv;
    private PopupWindow mPopWindow;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pop);
        tv= (TextView) findViewById(R.id.tv);
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showPopupWindow();
            }
        });

    }

    private void showPopupWindow() {
        //设置contentView
        View contentView = LayoutInflater.from(PopActivity.this).inflate(R.layout.popwind, null);
        mPopWindow = new PopupWindow(contentView,
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        mPopWindow.setContentView(contentView);
        //设置各个控件的点击响应
        TextView tv1 = (TextView)contentView.findViewById(R.id.pop_computer);
        TextView tv2 = (TextView)contentView.findViewById(R.id.pop_financial);
        tv1.setOnClickListener(this);
        tv2.setOnClickListener(this);
        //当点击popwindow以外的地方关闭窗口
        mPopWindow.setBackgroundDrawable(new BitmapDrawable());
        mPopWindow.setOutsideTouchable(true);
        View rootview = LayoutInflater.from(PopActivity.this).inflate(R.layout.activity_pop, null);
        //显示的位置
        mPopWindow.showAtLocation(rootview, Gravity.BOTTOM, 200, 300);

    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id){
            case R.id.pop_computer:{
                // 点击事件
                mPopWindow.dismiss();//关闭窗口
            }
            break;
            case R.id.pop_financial:{
                // 点击事件
                mPopWindow.dismiss();//关闭窗口
            }
        }
    }


}

这样就轻轻松松实现了 

对应的动画请参考

http://www.cocoachina.com/android/20180224/22336.html

猜你喜欢

转载自blog.csdn.net/bbtianshi/article/details/81912554