对话框布局与 android 6.0 不同

Dialog layout is different with android 6.0

我最近更新了我的应用程序以满足 Android M (api 23) 个新更改,我注意到对话框的呈现方式发生了巨大变化。

特别是保留给标题的 space 消失了,并且不遵守宽度和高度方面的限制。

所有对话框都会发生这种情况,特别是在下面,您可以看到相同版本的注销对话框安装在 api 22(左)与 api 23(右)设备上的外观。

如何在所有设备上实现相同的表示?

这是对话框的自定义 xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="16dp">
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="text message"
            android:id="@+id/confirmDialogMessage"
            android:layout_marginTop="8dp" />
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:weightSum="1"
        android:background="@color/blue_mp" >

        <ImageButton
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/confirmDialogNo"
            android:layout_weight="0.5"
            android:scaleType="fitStart"
            android:src="@drawable/cancelicon"
            android:layout_marginLeft="20dp"
            android:background="@android:color/transparent" />

        <ImageButton
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/confirmDialogYes"
            android:layout_weight="0.5"
            android:src="@drawable/okicon"
            android:scaleType="fitEnd"
            android:layout_marginRight="20dp"
            android:background="@android:color/transparent" />
    </LinearLayout>
</LinearLayout>

对话框是这样显示的:

dialog = new Dialog(v.getContext());
dialog.setContentView(R.layout.dialog_confirm_refuse_layout);
dialog.setTitle(getString(R.string.options_logout));
dialog.show();

编辑:您能否也解释一下为什么这种渲染变化发生在 api 22 和 api 23 之间?

您可以在对齐按钮时从线性布局更改为相对布局,并将图像按钮更改为图像视图。

附上示例代码:

<RelativeLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:background="@color/pricing_blue" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/confirmDialogNo"
        android:src="@drawable/button_green"
        android:layout_marginLeft="20dp"
        android:layout_alignParentLeft="true"
        android:background="@android:color/transparent" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/confirmDialogYes"
        android:src="@drawable/button_green"
        android:layout_marginRight="20dp"
        android:layout_alignParentRight="true"
        android:background="@android:color/transparent" />
</RelativeLayout>

使您的父布局相对。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <LinearLayout
            android:id="@+id/text"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="16dp">
        <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="text message"
                android:id="@+id/confirmDialogMessage"
                android:layout_marginTop="8dp" />
    </LinearLayout>

    <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:weightSum="1"
            android:layout_below="@id/text"
            android:background="@color/secondary_blue" >

        <ImageButton
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/invite"
                android:layout_weight="0.5"
                android:scaleType="fitStart"
                android:src="@drawable/y_cross_btn"
                android:layout_marginLeft="20dp"
                android:background="@android:color/transparent" />

        <ImageButton
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:id="@+id/close"
                android:layout_weight="0.5"
                android:src="@drawable/ic_tick_btn"
                android:scaleType="fitEnd"
                android:layout_marginRight="20dp"
                android:background="@android:color/transparent" />
    </LinearLayout>
</RelativeLayout>