Tengo algún problema con el margen inferior en diferentes dispositivos. ¿Cómo obtener el mismo margen inferior para dispositivos con / sin botones en pantalla?

Implementación de PopupWindow:

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);

Nexus 6

enter image description here

Samsung 5

enter image description here

<?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>
0
phnmnn 15 dic. 2016 a las 16:57

2 respuestas

La mejor respuesta

Aquí hay una solución:

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);
    }
0
phnmnn 15 dic. 2016 a las 14:53

Elimine match_parent de layout_height y reemplácelo con wrap_content así

<?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>
0
Malik Abu Qaoud 15 dic. 2016 a las 14:03