1、PopupWindow
1-1、基本使用
if (mPopupWindow == null) {
View layout = LayoutInflater.from(MainActivity.this)
.inflate(R.layout.dialog_layout, null);
mPopupWindow = new PopupWindow();
mPopupWindow.setContentView(layout);
mPopupWindow.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
mPopupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
mPopupWindow.setFocusable(true);
mPopupWindow.setOutsideTouchable(false);
mPopupWindow.setBackgroundDrawable(new BitmapDrawable(getResources(), (Bitmap) null));
mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
}
});
mPopupWindow.setAnimationStyle(R.style.dialogWindowAnimator);
}
if (!mPopupWindow.isShowing()) {
mPopupWindow.showAtLocation(findViewById(R.id.btnOkHttp), Gravity.BOTTOM, 0, 0);
}
1-2、适配虚拟按键
如果是华为手机类似这种存在虚拟按键的手机,需要加上
mPopupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
3、BottomSheetDialog
首先引入依赖
implementation 'com.android.support:design:28.0.0'
使用代码很简单,由于是Dialog的子类,所以调用上差不多。
BottomSheetDialog dialog = new BottomSheetDialog(MainActivity.this);
dialog.setContentView(R.layout.dialog_layout);
dialog.show();
上面很简单的三句话,就实现了之前Android 弹出式布局之Dialog的使用中自下而上的带动画的弹框功能了。
3、PopupMenu
第一种使用方法getMenu()
添加
mPopupMenu = new PopupMenu(MainActivity.this, v);
mPopupMenu.getMenu().add("第一个");
mPopupMenu.getMenu().add("第二个");
mPopupMenu.getMenu().add("第三个");
mPopupMenu.show();
第二种使用方法``````添加
mPopupMenu = new PopupMenu(MainActivity.this, v);
mPopupMenu.getMenuInflater().inflate(R.menu.popup_menu, mPopupMenu.getMenu());
mPopupMenu.show();
menu文件
//popup_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/item1"
android:title="这是第一个item" />
<item
android:id="@+id/item2"
android:title="这是第二个item" />
<item
android:id="@+id/item3"
android:title="这是第三个item" />
</menu>
效果如下:
4、几个常用的工具方法
/**
* Desc: 获取虚拟按键高度 放到工具类里面直接调用即可
*/
public static int getNavigationBarHeight(Context context) {
int result = 0;
if (hasNavBar(context)) {
Resources res = context.getResources();
int resourceId = res.getIdentifier("navigation_bar_height", "dimen", "android");
if (resourceId > 0) {
result = res.getDimensionPixelSize(resourceId);
}
}
return result;
}
/**
* 检查是否存在虚拟按键栏
*/
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public static boolean hasNavBar(Context context) {
Resources res = context.getResources();
int resourceId = res.getIdentifier("config_showNavigationBar", "bool", "android");
if (resourceId != 0) {
boolean hasNav = res.getBoolean(resourceId);
// check override flag
String sNavBarOverride = getNavBarOverride();
if ("1".equals(sNavBarOverride)) {
hasNav = false;
} else if ("0".equals(sNavBarOverride)) {
hasNav = true;
}
return hasNav;
} else { // fallback
return !ViewConfiguration.get(context).hasPermanentMenuKey();
}
}
/**
* 判断虚拟按键栏是否重写
*/
private static String getNavBarOverride() {
String sNavBarOverride = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
try {
Class c = Class.forName("android.os.SystemProperties");
Method m = c.getDeclaredMethod("get", String.class);
m.setAccessible(true);
sNavBarOverride = (String) m.invoke(null, "qemu.hw.mainkeys");
} catch (Throwable e) {
}
}
return sNavBarOverride;
}