【问题标题】:Android: Change text color in ListView for singleChoiceAndroid:在 ListView 中为 singleChoice 更改文本颜色
【发布时间】:2019-02-08 13:44:17
【问题描述】:

我有一个列表视图设置为使用 singleChoice。我要做的就是将默认背景颜色更改为白色,将文本颜色更改为黑色。我无法弄清楚如何做到这一点。这是我的 xml 布局:

<ListView
    android:id="@+id/lvSpeeds"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@id/llToolbar"
    android:layout_below="@id/rgSpeedUnits"
    android:textColor="@android:color/black"
    android:choiceMode="singleChoice"
    android:background="#ffffff"
    android:cacheColorHint="#00ffffff"
    android:clickable="true"
    android:divider="#ff000000"
    android:dividerHeight="1dp"
    android:focusable="true"
    android:scrollingCache="true" />

编辑:我应该指出我只想使用 xml 布局文件而不是代码来更改它。我已经知道如何在代码中做到这一点。使用 android.R.layout.simple_list_item_single_choice 以外的自定义布局会强制您实现适配器、绑定、编写更多代码等等。从查看更多帖子来看,似乎无法仅使用 xml 更改文本颜色。事实上,由于底层布局 android.R.layout.simple_list_item_single_choice 无法访问,因此似乎无法更改一行中的任何内容。

【问题讨论】:

  • 为 textView 创建不同的布局,设置您想要的文本颜色,并在您使用的适配器中使用此布局。看看这个link它会对你有所帮助。

标签: android listview


【解决方案1】:

对于列表视图使用 android 选择器 像这样

并用anyname.xml 保存它,并参考您的列表背景

 <selector xmlns:android="http://schemas.android.com/apk/res/android" >

     <item android:state_pressed="true" android:drawable="@drawable/about_btn_hover"></item>
    <item android:drawable="@drawable/about_btn"></item> 

</selector>

为了改变文本颜色,在你的 res 目录中添加颜色文件夹 创建一个xml保存它textchangecolor.xml 并在其中添加以下几行

    <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" 
        android:color="@color/whiteColor"></item>
    <item android:color="@color/bluetxt"></item> 
</selector>

并引用文本颜色

【讨论】:

  • 也许您想将 about_btn 与您遗漏的所有其他常量一起包含在内。事实上,你的代码是没用的。
  • 我刚刚添加了 xmls 而不是代码,选择器将自动选择其状态,其行为为默认和 state_pressed。
  • 你的代码没用。没有人可以复制和粘贴它并期望它能够工作。请删除您的解决方案。你在浪费我的时间。
  • 我对这个解决方案的唯一问题是 ListView 没有 textColor 属性。
  • 谢谢Usman,我想我明白了,我将相应视图的样式从android:listViewStyle 更改为@style/ListViewChangeSelectedTextColor,并将以下代码添加到我的styles.xml 文件中:&lt;style name ="ListViewChangeSelectedTextColor" parent="@android:style/Widget.ListView"&gt; &lt;item name="android:textColor"&gt;@drawable/select_text_white_else_black&lt;/item&gt; &lt;/style&gt;跨度>
【解决方案2】:

试试这个:

在 getview mathod 的适配器中插入此代码:

LinearLayout mRowLayout = (LinearLayout) vi
                    .findViewById(R.id.list_item_layout);
            final TextView title = (TextView) vi
                    .findViewById(R.id.list_item);

            title.setText(Massage[position]);

            mRowLayout.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {


                                    title.setTextColor(Color.RED);

                            });

这里list_item代码:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list_item_layout"
android:orientation="vertical" >


<RelativeLayout android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/row_single"
                android:layout_margin="5dp">


<TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:textColor="@android:color/black"
        android:padding="10dp"
        android:textSize="16sp"

        android:layout_toLeftOf="@+id/arrow"
        android:id="@+id/list_item"/>
</RelativeLayout>
</LinearLayout>

【讨论】:

  • 这需要为适配器创建代码,添加 RadioButtons 并添加更多代码来处理单选按钮。寻找一个纯xml的解决方案。
【解决方案3】:

我能找到的最佳解决方案是使用 styles.xml 并为我的对话框设置主题。

styles.xml

<style name="DialogTheme" parent="AppTheme">
    <item name="android:textColor">@color/text</item>
    <item name="android:textColorAlertDialogListItem">@color/text</item>
    + other styles
</style>

在 Java 内置对话框中:

ContextThemeWrapper theme;
theme = ContextThemeWrapper(view.getContext(), R.style.DialogTheme);
AlertDialog.Builder builder = new AlertDialog.Builder(theme);

在 XML 构建的对话框中:

<myLayout
    ....
    android:theme="@style/DialogTheme" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    相关资源
    最近更新 更多