【问题标题】:Inflated AlertDialogs unreadable on Android 4.X while looking perfect on Android 2.X膨胀的 AlertDialogs 在 Android 4.X 上无法读取,而在 Android 2.X 上看起来很完美
【发布时间】:2012-10-03 16:30:12
【问题描述】:

为了显示具有丰富内容和输入元素的对话框,我使用Android的AlertDialog.Builder创建AlertDialog的对象,然后使用系统的LayoutInflater将内容(setView(...))设置为一些XML布局文件。

这在 Android 2.X 上看起来很完美(深色背景,白色文本):

但在 Android 4.X 上它根本不可读:

注意:当然,这些对话框并不相同,但两个对话框都会出现问题。

那我做错了什么?我在 API 级别 Theme.Light,在 >= 11 上使用 Theme.Holo.Light

编辑 1:这是我用于充气的代码:

private LayoutInflater mGlobalInflater; // in Activity class
mGlobalInflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE); // in Activity's onCreate()
mGlobalInflater.inflate(R.layout.input_dialog, null); // when needed

编辑 2:这是“选择日期”视图的膨胀 XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:padding="15dp">
    <DatePicker
        android:id="@+id/input_date"
        android:startYear="1900"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="2dp"
        android:text="@string/hint" />
</LinearLayout>

编辑 3: 我现在发现,当使用 mGlobalInflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE); 时,膨胀的对话框内容在 Android = 3.0 上使用时正确呈现mInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);。所以Context 与众不同。为什么?

编辑 4:

对话框在onContextItemSelected()Activity 内部创建和扩展:

AlertDialog.Builder dialogPoup = new AlertDialog.Builder(MainActivity.this);
dialogPoup.setTitle("abc123");
final View inputDialog = mGlobalInflater.inflate(R.layout.input_dialog, null);
// setting some properties of the view and its child views
dialogPoup.setView(inputDialog);

这是正确的吗?或者我是否必须将MainActivity.this 替换为Activity 的上下文在onCreate() 中分配的变量mContext(例如)?

【问题讨论】:

  • 而不是发布标准的通胀代码,xml文件怎么样?
  • 这也没什么特别的 ;)
  • 不久前我遇到了类似的事情。尝试在布局上明确设置 textcolor。你必须为不同的 api 版本创建不同的布局,但我认为这是你必须做的。
  • 没有别的办法了吗?我的意思是,我使用标准主题和简单的 XML 进行布局。那么为什么看起来这么糟糕呢? API 级别 >= 11 是否不再支持在对话框中膨胀视图?
  • 是的。文档希望人们改用对话片段,但我认为这不会有所作为。看看您是否可以在主题中为 api > 11 指定自定义文本颜色 - 您可能必须为其创建样式并在主题中设置属性,例如 android:textViewStyle 或其他内容。

标签: android android-layout android-theme android-inflate android-version


【解决方案1】:

不要像上面的代码那样使用getApplicationContext() 来获取 LayoutInflater,而是像这样使用 Activity 中的那个:

    mGlobalInflater = LayoutInflater.from(this); // in Activity's onCreate()

这应该有望为对话框使用匹配的主题。

【讨论】:

  • 谢谢,成功了! :) 但是我调用LayoutInflater 的方式是正确的(如文档中所引用)。您只需替换上下文:mGlobalInflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);(当然,也可以省略 this
  • 但不幸的是,正如我现在所看到的,它不再适用于 API 级别 11 之前的 Android 版本。在那里,文本现在是深色的,因此也不可读。
  • 它在 11 之前和之后的 API 级别对我来说都很好。你提到你有几个对话框,也许布局中有一些你没有附加到问题的特殊内容?他们都在同一个活动中吗?如果不是,他们每个人都应该从 Activity 的上下文中获取自己的 LayoutInflater。
  • 是的,它们都在同一个Activity。使用Application 的充气机时,您能否在 API 级别 >= 11 上重现错误行为?
  • 是的,在 ICS 上,如果我在您问题中的示例代码之后使用应用程序上下文,则会遇到白底白字问题。
【解决方案2】:

当您说您正在使用Theme.LightTheme.Holo.Light 时,您将这个主题应用在什么上很重要。

它是您的Application 还是您的Activity。如果您在Application 级别上应用它,那么如果您从应用程序中获得Context,那么您将使用该主题(与Activity 类似)。如果应用程序和活动的两个主题不同,您会注意到差异。

当您阅读上述@Joe 的回答时,请记住这一点。您提到@Joe 的回答解决了 api11+ 的问题,但之前的问题已经解决了。

因此,只需查看您的 AndroidManifest 并查看您对 &lt;application&gt; 的主题以及您要从中扩展 Dialog 的特定 &lt;activity&gt; 的主题。

希望有帮助

【讨论】:

  • 谢谢!对我来说,这也是合乎逻辑的,也是最合理的解决方案。但它不起作用。我已将相同的内容应用于活动和应用程序,但问题仍然存在。
【解决方案3】:

我发现的唯一可行的解​​决方案如下:

    LayoutInflater mInflater, mGlobalInflater;
    mInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    if (android.os.Build.VERSION.SDK_INT >= 11) {
        mGlobalInflater = mInflater;
    }
    else {
        mGlobalInflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
    }

现在您可以使用 mInflater 进行正常膨胀(例如:ListView 内的行)和 mGlobalInflater 用于膨胀 AlertDialog 内的视图(使用 setView())。这将适用于所有 API 级别。

【讨论】:

  • 我自己一直找不到更好的解决方案
猜你喜欢
  • 1970-01-01
  • 2019-09-24
  • 1970-01-01
  • 1970-01-01
  • 2012-04-28
  • 2012-09-16
  • 1970-01-01
  • 1970-01-01
  • 2015-11-23
相关资源
最近更新 更多