【问题标题】:Change text color of check box in a list view更改列表视图中复选框的文本颜色
【发布时间】:2020-07-26 17:00:06
【问题描述】:

所以我在一个列表数组中有一个复选框列表,其中填充了各种文本和一个自定义复选标记,当我单击特定项目时有效显示。但是,我似乎无法更改所检查项目中文本的文本颜色。我想将其更改为与复选标记相同的颜色。当我单击该项目并将文本颜色设置为下面的 textColor 代码时,文本变为粉红色!这不是我想要的颜色。它似乎是默认颜色。我的问题是如何更改嵌入在列表视图中的特定复选框的文本颜色?我已经在这个网站和其他网站上进行了搜索,但我找不到可以帮助我的答案。

文字颜色

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:textColor="@color/purple_color" />
</selector>

和紫色复选标记:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@android:color/transparent" android:state_checked="false"/>
    <item android:drawable="@drawable/ic_check_black_24dp" android:state_checked="true"/>
</selector>

和活动中的列表视图:

<ListView
            android:id="@+id/myList"
            android:paddingTop="12dp"
            android:layout_marginLeft="44dp"
            android:layout_marginRight="44dp"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />

以及单个列表项:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <CheckBox
        android:id="@+id/orientation_type"
        android:layout_width="fill_parent"
        android:layout_height="44dp"
        android:gravity="left|center_vertical"
        android:textColor="@drawable/check_box_text_color_purple"
        android:textSize="17dp"
        android:layoutDirection="rtl"
        android:button="@drawable/check_purple" />

</LinearLayout>

和数组适配器:

public class orientation_adapter extends ArrayAdapter<orientation_item> {

    private Context mContext;
    int mResource;

    public orientation_adapter(@NonNull Context context, int resource, @NonNull ArrayList<orientation_item> objects) {
        super(context, resource, objects);
        this.mContext = context;
        this.mResource = resource;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        String orientation = getItem(position).getOrientation();

        gender_item user_orientation = new gender_item(orientation);

        LayoutInflater inflator = LayoutInflater.from(mContext);
        convertView = inflator.inflate(mResource, parent, false);

        //TextView orientation_type = (TextView) convertView.findViewById(R.id.orientation_type);
        CheckBox orientation_type = (CheckBox) convertView.findViewById(R.id.orientation_type);

        orientation_type.setText(orientation);

        return convertView;

    }

}

和主要活动:

ArrayList<orientation_item> list = new ArrayList<>();

 //items created and added here

 adapter = new orientation_adapter(ChooseOrientation.this, R.layout.orientation_unit, list);

        myList.setAdapter(adapter);

【问题讨论】:

标签: java android-studio android-listview android-arrayadapter


【解决方案1】:

我最终将此应用于列表视图的单个组件 xml 中的 android:textColor 字段。这解决了问题,但与我之前所做的类似。当您使用实际颜色代码而不是链接到颜色时,Android 似乎反应更好。

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="false" android:color="#000000"/>
    <item android:state_checked="true" android:color="#FF6C63FF"/> <!-- checked -->
    <item android:color="#ffffff"/> <!-- anything else -->
</selector>

【讨论】:

    猜你喜欢
    • 2013-10-17
    • 1970-01-01
    • 1970-01-01
    • 2021-05-24
    • 1970-01-01
    • 2013-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多