简体   繁体   English

更改列表视图中复选框的文本颜色

[英]Change text color of check box in a list view

So I have a list of checkboxes in a list array populated with various text and a custom checkmark that effectively shows when I click the specific item.因此,我在列表数组中有一个复选框列表,其中填充了各种文本和一个自定义复选标记,当我单击特定项目时有效显示。 However, I can't seem to change the text color of the text in the item that is checked.但是,我似乎无法更改所检查项目中文本的文本颜色。 I want to change it to the same color as the checkmark.我想将其更改为与复选标记相同的颜色。 When I do click on the item and when the text color is set to the textColor code below, the text turns pink.当我单击该项目并将文本颜色设置为下面的 textColor 代码时,文本变为粉红色。 which is not the color I want.这不是我想要的颜色。 It seems to be a default color?它似乎是默认颜色? My question is then how do I change the textColor of the particular checkbox that I have checked that is embedded in a listview.那么我的问题是如何更改嵌入在列表视图中的特定复选框的文本颜色。 I have searched on this website and others but I couldn't find an answer that could help me.我已经在这个网站和其他网站上进行了搜索,但我找不到可以帮助我的答案。

text color文字颜色

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

and purple checkmark:和紫色复选标记:

<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>

and list view in the activity:并在活动中列出视图:

<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" />

and the individual list item:和单个列表项:

<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>

and the array adapter:和阵列适配器:

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;

    }

}

and the main activity:和主要活动:

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);

I ended up applying this to the android:textColor field in the individual component xml of the listview.我最终将其应用于列表视图的单个组件 xml 中的 android:textColor 字段。 This solved the problem but is similar to what I did before.这解决了问题,但与我之前所做的类似。 Android seems to respond better when you use actual color codes instead of linking to colors.当您使用实际颜色代码而不是链接到 colors 时,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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM