简单的菜单弹框


1. 先来个效果图

 


2. 在MainActivity 里面的代码 


public class MainActivity extends AppCompatActivity {

    private LinearLayout login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button) findViewById(R.id.bt);
        login = (LinearLayout) findViewById(R.id.ll_login);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showSwitch(login);
            }
        });

    }

private void showSwitch(View parentView) {
    LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View contentview = inflater.inflate(R.layout.popup_switch, null);
    TextView tvSwitch = (TextView) contentview.findViewById(R.id.tv_switch);
    TextView tvSignOut = (TextView) contentview.findViewById(R.id.tv_sign_out);

    contentview.setFocusable(true); // 这个很重要
    contentview.setFocusableInTouchMode(true);
    final PopupWindow popupWindow = new PopupWindow(contentview, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    popupWindow.setFocusable(true);
    popupWindow.setOutsideTouchable(false);
    contentview.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                popupWindow.dismiss();

                return true;
            }
            return false;
        }
    });
    tvSwitch.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent =new Intent(getApplicationContext(),SecActivity.class);
            startActivity(intent);
            popupWindow.dismiss();
        }
    });

    tvSignOut.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent =new Intent(getApplicationContext(),ThridActivity.class);
            startActivity(intent);
            popupWindow.dismiss();
        }
    });
    popupWindow.showAsDropDown(parentView, 400,0); //在点击按钮的位置

}

3.    activity_main.xml 的代码

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    tools:context="com.jgw.showpop.MainActivity">
    
    <LinearLayout
        android:layout_marginTop="50dp"
        android:id="@+id/ll_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:gravity="center"
        >
        <Button
            android:id="@+id/bt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="提交"
            />
    </LinearLayout>


</LinearLayout>

 

4.   popup_switch.xml  的代码


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@mipmap/icon_menu_off"
    android:paddingTop="10dp"
    android:orientation="vertical">
    
    <TextView
        android:id="@+id/tv_switch"
        android:textSize="14sp"
        android:textColor="#323232"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawableLeft="@mipmap/icon_switch"
        android:drawablePadding="10dp"
        android:gravity="center"
        android:padding="15dp"
        android:text="设置" />
    <View android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_width="match_parent"
        android:layout_height="1px"
        android:background="#dddddd" />
    <TextView
        android:id="@+id/tv_sign_out"
        android:textSize="14sp"
        android:textColor="#323232"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawableLeft="@mipmap/icon_sign_out"
        android:drawablePadding="10dp"
        android:gravity="left"
        android:padding="15dp"
        android:text="菜单" />
</LinearLayout>


  代码完成 , 仅供参考


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值