Android_Popupwindow

代码块:

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    PopupWindow pop;
    private RelativeLayout rl;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rl=(RelativeLayout)findViewById(R.id.rl);
    }

    public void click(View view) {
        //加载布局文件
        View inflate = View.inflate(this, R.layout.pw_layout, null);
        TextView tv=(TextView) inflate.findViewById(R.id.tv_1);
        TextView tv2=(TextView) inflate.findViewById(R.id.tv_2);
        TextView tv3=(TextView) inflate.findViewById(R.id.tv_3);
        TextView tv4=(TextView) inflate.findViewById(R.id.tv_4);
        tv.setOnClickListener(this);
        tv2.setOnClickListener(this);
        tv3.setOnClickListener(this);
        tv4.setOnClickListener(this);

        //创建一个PopupWindow,设置宽高,把布局文件加载过来
        pop=new PopupWindow(inflate, AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT);
        pop.setBackgroundDrawable(new ColorDrawable());//给pop设置背景
        pop.setOutsideTouchable(true);//设置外部可触摸事件
        pop.showAtLocation(rl, Gravity.BOTTOM,0,0);//设置pop显示的位置
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_1:
                Toast.makeText(MainActivity.this,"这就是历史记录", Toast.LENGTH_SHORT).show();
                break;
            case R.id.tv_2:
                rl.setBackgroundColor(Color.GRAY);
                break;
            case R.id.tv_3:

                break;
            case R.id.tv_4:

                break;
        }
        if(pop.isShowing()){
            pop.dismiss();
        }
    }
}

布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:id="@+id/rl">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="呼叫弹出窗口"
        android:textSize="30sp"
        android:onClick="click" />

</RelativeLayout>

Popupwindow布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
    
<TextView 
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="wrap_content"
    android:drawableTop="@mipmap/emoji_015"
    android:text="历史"
    android:gravity="center"
    android:id="@+id/tv_1"
    android:clickable="true"/>
<TextView 
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="wrap_content"
    android:drawableTop="@mipmap/emoji_016"
    android:text="设置"
    android:gravity="center"
    android:id="@+id/tv_2"
    android:clickable="true"/>
<TextView 
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="wrap_content"
    android:drawableTop="@mipmap/emoji_017"
    android:text="下载"
    android:gravity="center"
    android:id="@+id/tv_3"
    android:clickable="true"/>
<TextView 
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="wrap_content"
    android:drawableTop="@mipmap/emoji_018"
    android:text="帮助"
    android:gravity="center"
    android:id="@+id/tv_4"
    android:clickable="true"/>
</LinearLayout>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值