Android:如何正确设置AlertDialog中列表项的文本颜色
我的应用程序中有一个Android:如何正确设置AlertDialog中列表项的文本颜色,android,themes,android-alertdialog,Android,Themes,Android Alertdialog,我的应用程序中有一个AlertDialog。它包含一个自定义视图列表,其中包含TextView小部件。在安卓2.x上一切正常。AlertDialog是用白名单和黑文本创建的。但当我在安卓3.x设备上运行我的应用程序时,所有的TextViews都是黑色的,列表的背景也是黑色的。因此,在点击并按住其中一个项目之前,我无法看到文本 以下是布局文件中的文本视图的定义: <TextView android:id="@+id/label" android:layout_width="f
AlertDialog
。它包含一个自定义视图列表,其中包含TextView
小部件。在安卓2.x上一切正常。AlertDialog
是用白名单和黑文本创建的。但当我在安卓3.x设备上运行我的应用程序时,所有的TextView
s都是黑色的,列表的背景也是黑色的。因此,在点击并按住其中一个项目之前,我无法看到文本
以下是布局文件中的文本视图的定义:
<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" />
我认为对textpearancesmallinverse
属性使用textpearancesmallinverse
是设置文本参数的正确方法,它必须在所有设备上工作,但似乎我错了。那么,我应该怎么做才能使AlertDialog
在所有平台上正确显示列表项呢?提前感谢。这可能是因为您没有指定主题,然后会返回到默认主题。在2.x中应该是Theme.Black,在3.x中应该是Theme.Holo(或者Theme.Light,这个不确定)。然后textAppearanceSmallInverse
解析为每个主题中的不同样式。弹出对话框的代码应类似于以下内容:
// Sets dialog for popup dialog list
AlertDialog dialog;
String[] items = {"exampleItem"};
ListAdapter itemlist = new ArrayAdapter(this, android.R.layout.simple_list_item_1, items);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Title");
builder.setAdapter(itemlist, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int item)
{
}
});
dialog = builder.create();
dialog.getListView().setBackgroundColor(Color.WHITE);
在这里,您将获得listview并将背景色设置为白色。如果要更改每个文本视图的文本颜色,则需要在文本视图的布局中定义它们的颜色,在本例中为黑色:
android:textColor="#000000"
解决方案是利用Android内置的资源选择系统。您应该指定两种不同的样式,并根据API版本将它们放置在适当的文件夹中。请注意,以下示例不是我的,我是从教程中选取的
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>
<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
android:style="@style/MyListTextAppearanceSmall"
android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="marquee" />
有关更详细的解释,请参阅上面链接的教程。公认的答案似乎有点过火。我只是通过调用以下命令强制反转背景:
dialogBuilder.setInverseBackgroundForced(true);
很好地解决了这个问题。我在2.x中使用了“Theme”,在3.x中使用了“Theme.Holo”。但问题是“AlertDialog”在不同的平台上使用相反的样式。但两个平台对标题文本使用相同的样式。这是奇怪的行为。谢谢你们的回复,但我不想改变名单的背景。我希望它看起来像应该在一个特定的平台上。我只想设置一个合适的文本样式,使它在每个平台上都可读。这个问题已经很老了,但也许其他人会从中受益。解决方案是利用安卓内置的资源选择来实现您的目标。有关说明,请参阅教程。我在项目中使用了完全相同的方法,但希望有更好的解决方案。我还没有看到您的答案,但是如果你发布了,我会接受。我上面的评论是从我的答案中提取的。也许你发布了一条评论而不是答案?据我记忆所及,这个标志并不能解决Android 2.x和Android 3.x上的问题。setInverseBackgroundForced现在已经被弃用了