我的应用程序中有一个 AlertDialog
。它包含一个自定义 View 列表,其中包含 TextView
小部件。在 Android 2.x 上一切正常。 AlertDialog
是用白名单和黑色文本创建的。但是当我在 Android 3.x 设备上运行我的应用程序时,所有 TextView
都是黑色的,列表的背景也是黑色的。因此,在我点击并按住其中一项之前,我无法看到文本。
这是来自布局文件的 TextView
定义:
<TextView
android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="marquee"
android:textAppearance="?android:attr/textAppearanceSmallInverse" />
我认为为 textAppearance
属性使用 textAppearanceSmallInverse
是设置文本参数的正确方法,它必须适用于所有设备,但看来我错了。那么我应该怎么做才能使 AlertDialog
在所有平台上正确显示列表项?提前致谢。
最佳答案
解决方案是利用 Android 的内置资源选择系统。您应该指定两种不同的样式,并根据 API 版本将它们放在适当的文件夹中。注意下面的例子不是我的,我是从this拿来的教程。
res/values-v4/styles.xml
:
<resources>
<!-- Text for listboxes, inverted for Andorid prior to 3.0 -->
<style name="MyListTextAppearanceSmall">
<item name="android:textAppearance">?android:attr/textAppearanceSmallInverse</item>
</style>
<style name="MyListTextAppearanceDefault">
<item name="android:textAppearance">?android:attr/textAppearanceInverse</item>
</style>
<style name="MyListTextAppearanceMedium">
<item name="android:textAppearance">?android:attr/textAppearanceMediumInverse</item>
</style>
</resources>
res/values-v11/styles.xml
:
<resources>
<!-- Text for listboxes, non-inverted starting with Android 3.0 -->
<style name="MyListTextAppearanceSmall">
<item name="android:textAppearance">?android:attr/textAppearanceSmall</item>
</style>
<style name="MyListTextAppearanceDefault">
<item name="android:textAppearance">?android:attr/textAppearance</item>
</style>
<style name="MyListTextAppearanceMedium">
<item name="android:textAppearance">?android:attr/textAppearanceMedium</item>
</style>
</resources>
然后,在您的 TextView
中,像这样指定样式:
<TextView
android:style="@style/MyListTextAppearanceSmall"
android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="marquee" />
有关详细说明,请参阅上面链接的教程。
关于Android:如何正确设置 AlertDialog 中列表项的文本颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6698350/