简单的菜单弹框
来源:互联网 发布:日本战国 知乎 编辑:程序博客网 时间:2024/06/12 01:50
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>
代码完成 , 仅供参考
阅读全文
0 0
- 简单的菜单弹框
- CSS3实现的简单下拉框菜单
- 简单的树状菜单
- 简单的级联菜单
- 简单的二级菜单
- 菜单的简单应用
- 简单的菜单
- 简单的联动菜单
- 简单的菜单
- 最简单的菜单
- 汇编程序:简单的菜单
- 简单的横向菜单测试
- 一个简单的收缩菜单
- HTML简单的下拉菜单
- 一个简单的javascript菜单
- 简单的二级联动菜单
- treeview简单的右键菜单
- 一个简单的CSS菜单
- hdu5248序列变换 二分查找 贪心
- python-基本数据类型之字典
- Android的基础控件
- IOS边下边播
- 一维数组和二维数组的使用
- 简单的菜单弹框
- JavaScript 时间操作
- .net SqlBulkCopy 批量添加
- java持久层框架mybatis如何防止sql注入
- 生产者消费者注意细节
- 数据采集器程序开发PDA软件开发定制盘点机条码采集系统软件
- caffe小问题汇总(持续更新) PS:所有问题均在caffe-windows下产生 1、为什么AlexNet中,InnerProduct_Layer(fc8)层的输出可以直接作为Accuracy
- linux终端分屏软件tmux使用简略方法
- Linux配置完JDK后出现bash: /usr/bin/java: /lib/ld-linux.so.2: bad ELF interpreter: 没有那个文件或目录