Android PopupWindow - 如何为带有屏幕按钮的设备获得相同的底部边距
Android PopupWindow - how to get same bottom margin for devices with on-screen buttons
我在不同设备上遇到底部边距问题。
如何为设备 with/without 屏幕按钮获得相同的底部边距?
弹出窗口的实现:
DisplayMetrics dm = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
View viewGroup= activity.getLayoutInflater().inflate(R.layout.my_dialog, null, false);
popupWindow = new PopupWindow(viewGroup, width, height);
连结 6
三星 5
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#AA000000"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="15dp"
android:background="#fff"
android:layout_weight="1">
...
</LinearLayout>
</LinearLayout>
从 layout_height 中删除 match_parent 并将其替换为 wrap_content 像这样
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#AA000000">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:orientation="vertical"
android:background="#fff"
android:layout_weight="1">
...
</LinearLayout>
解决方法如下:
if (hasNavBar(activity.getResources())){
LinearLayout linearLayout = (LinearLayout) popupWindow.getContentView().findViewById(R.id.linear_layout_dialog);
if( linearLayout.getLayoutParams() instanceof ViewGroup.MarginLayoutParams)
{
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) linearLayout.getLayoutParams();
lp.bottomMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
(float) 43, activity.getResources().getDisplayMetrics());
linearLayout.setLayoutParams(lp);
}
}
public static boolean hasNavBar (Resources resources)
{
int id = resources.getIdentifier("config_showNavigationBar", "bool", "android");
return id > 0 && resources.getBoolean(id);
}
我在不同设备上遇到底部边距问题。 如何为设备 with/without 屏幕按钮获得相同的底部边距?
弹出窗口的实现:
DisplayMetrics dm = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
View viewGroup= activity.getLayoutInflater().inflate(R.layout.my_dialog, null, false);
popupWindow = new PopupWindow(viewGroup, width, height);
连结 6
三星 5
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#AA000000"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="15dp"
android:background="#fff"
android:layout_weight="1">
...
</LinearLayout>
</LinearLayout>
从 layout_height 中删除 match_parent 并将其替换为 wrap_content 像这样
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#AA000000">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:orientation="vertical"
android:background="#fff"
android:layout_weight="1">
...
</LinearLayout>
解决方法如下:
if (hasNavBar(activity.getResources())){
LinearLayout linearLayout = (LinearLayout) popupWindow.getContentView().findViewById(R.id.linear_layout_dialog);
if( linearLayout.getLayoutParams() instanceof ViewGroup.MarginLayoutParams)
{
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) linearLayout.getLayoutParams();
lp.bottomMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
(float) 43, activity.getResources().getDisplayMetrics());
linearLayout.setLayoutParams(lp);
}
}
public static boolean hasNavBar (Resources resources)
{
int id = resources.getIdentifier("config_showNavigationBar", "bool", "android");
return id > 0 && resources.getBoolean(id);
}