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>
代码完成 , 仅供参考