【发布时间】: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);
【问题讨论】:
-
stackoverflow.com/questions/27713829/… 这对你有帮助吗..
-
我最终解决了这个问题。虽然不太确定出了什么问题。这是一篇好文章,虽然可能会有所帮助。
标签: java android-studio android-listview android-arrayadapter