【问题标题】:AlertDialog.Builder.setView() will load wrong resource on LollipopAlertDialog.Builder.setView() 将在 Lollipop 上加载错误的资源
【发布时间】:2015-06-08 05:07:55
【问题描述】:

我正在为不同的 API 平台创建不同的对话框布局,方法是为高于 Android HoneyComb 的平台指定 res/layout-v11,而更早的设备应该从默认的 res/layout 目录加载。但是,在我更新我的 target-api 和支持库后,这种机制似乎被打破了。最终的 APK 在我的 Android 4.1 NexusS 上运行良好,但会在我的 Nexsu5 和 Nexus7 上加载默认布局,两者都是 Android 5.1.1。

这是一个奇怪的问题,因为与layout-vX区分的活动布局仍然有效,只是对话框布局错误。

示例代码:

MainActivity.java:(我尝试了android.app.AlertDialog和android.support.v7.app.AlertDialog,行为是一样的)

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.app.AlertDialog;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        View dialogView = View.inflate(this, R.layout.alert, null);
        builder.setView(dialogView);
        builder.show();
    }
}

res/layout/alert.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_margin="2.0dip"
    android:background="@android:color/white">

    <TextView
        android:id="@+id/web_site_add_terms_contents"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="16dp"
        android:textStyle="bold"
        android:text="alert box from res/layout"
        android:color="@android:color/black" />
    <CheckBox 
        android:id="@+id/web_site_add_terms_agree"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="16dp"
        android:layout_below="@id/web_site_add_terms_contents"
        android:text="check box"
        android:color="@android:color/black" />

</RelativeLayout>

res/layout-v11/alert.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_margin="2.0dip">

    <TextView
        android:id="@+id/web_site_add_terms_contents"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="16dp"
        android:textStyle="bold"
        android:text="alert box from res/layout-v11" />
    <CheckBox 
        android:id="@+id/web_site_add_terms_agree"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="16dp"
        android:layout_below="@id/web_site_add_terms_contents"
        android:text="check box v11" />

</RelativeLayout>

在 NexusS(4.1.2)、Nexus5(5.1.1) 和 Nexus7(5.1.1) 上简单构建并启动应用程序,结果如下:

  1. NexusS:显示“v11”文本的对话框
  2. Nexus5:显示默认文本的对话框
  3. Nexus7:显示默认文本的对话框

任何帮助或提示将不胜感激。


更新

我在 res/layout-v21/alert.xml 中添加了另一个资源文件夹,它适用于我的 Lollipop 设备!任何人都可以解释这个技巧吗?

res/layout-v21/alert.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_margin="2.0dip">

    <TextView
        android:id="@+id/web_site_add_terms_contents"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="16dp"
        android:textStyle="bold"
        android:text="alert box from res/layout-v21" />
    <CheckBox 
        android:id="@+id/web_site_add_terms_agree"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="16dp"
        android:layout_below="@id/web_site_add_terms_contents"
        android:text="check box v21" />

</RelativeLayout>

我构建了这个应用程序,它在我的 NexusS 上显示“v11”,在我的 Nexus5 和 Nexus7 上显示“v21”。我现在很困惑。

【问题讨论】:

    标签: android android-layout android-alertdialog android-support-library android-resources


    【解决方案1】:

    您可以通过将它们放在 layout-vxx 中来指定您的布局将应用哪个版本,其中 xx 是平台版本。例如,layout-v21 将适用于 Android 5.0 和更高版本,因此您的 Nexus 5(5.1.1,API 版本 22)将使用此文件夹中的布局。您的 Nexus S 处于 Android 版本 4.1.2(API 级别 16),因此它将使用 layout-v11 中的布局。 谷歌文档中的更多细节: http://developer.android.com/guide/topics/resources/providing-resources.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-06
      • 2012-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-16
      • 2017-07-15
      相关资源
      最近更新 更多