安卓 PopUpWindow实现

安卓 PopUpWindow

参考文章

1.下面是简单实例
main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent">
    <Button
            android:id="@+id/btn"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="show popWindow"/>
</LinearLayout>

2.然后是window的xml

<?xml version="1.0" encoding="utf-8"?>
 
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#ffffff"
        android:orientation="vertical"
        android:paddingBottom="2dp">
    <View
            android:layout_width="match_parent"
            android:layout_height="2.25dp"
            android:background="#fa7829"
            android:layout_alignParentTop="true"/>
 
    <TextView
            android:id="@+id/pop_computer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            style="@style/pop_text_style"
            android:text="计算机"/>
 
    <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@drawable/list_line"/>
 
    <TextView
            android:id="@+id/pop_financial"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            style="@style/pop_text_style"
            android:text="金融"/>
 
    <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@drawable/list_line"/>
 
    <TextView
            android:id="@+id/pop_manage"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            style="@style/pop_text_style"
            android:text="管理"/>
 
    <View
            android:layout_width="match_parent"
            android:layout_height="1dp"/>
 
</LinearLayout>

3.然后就是activity中的代码


public class MainActivity extends Activity implements View.OnClickListener{
 
    private PopupWindow mPopWindow;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
 
        Button btn = (Button) findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPopupWindow();
            }
        });
    }
 
    private void showPopupWindow() {
	    //设置contentView
        View contentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.popuplayout, null);
        mPopWindow = new PopupWindow(contentView,
                LayoutParams.WRAP_CONTENT, 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);
        TextView tv3 = (TextView)contentView.findViewById(R.id.pop_manage);
        tv1.setOnClickListener(this);
        tv2.setOnClickListener(this);
        tv3.setOnClickListener(this);
		//显示PopupWindow
        View rootview = LayoutInflater.from(MainActivity.this).inflate(R.layout.main, null);
        mPopWindow.showAtLocation(rootview, Gravity.BOTTOM, 0, 0);
 
    }
 
    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id){
            case R.id.pop_computer:{
                Toast.makeText(this,"clicked computer",Toast.LENGTH_SHORT).show();
                mPopWindow.dismiss();
            }
            break;
            case R.id.pop_financial:{
                Toast.makeText(this,"clicked financial",Toast.LENGTH_SHORT).show();
                mPopWindow.dismiss();
            }
            break;
            case R.id.pop_manage:{
                Toast.makeText(this,"clicked manage",Toast.LENGTH_SHORT).show();
                mPopWindow.dismiss();
            }
            break;
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值