【问题标题】:Edit row in listView, ArrayAdapter - Android在 listView、ArrayAdapter 中编辑行 - Android
【发布时间】:2015-02-26 15:55:20
【问题描述】:

如何更改单行文本的颜色?

这是我设置适配器并调用listView的代码:

 ArrayAdapter<String> adapter = new ArrayAdapter<String>(getListView().getContext(), R.layout.row, rez);
 getListView().setAdapter(adapter);

【问题讨论】:

    标签: android listview row android-arrayadapter


    【解决方案1】:

    您需要创建自己的适配器并在您想要的位置指定文本颜色:

    public class MyAdapter extends ArrayAdapter<String> {
    
        public MyAdapter(Context context, int resource, String[] strings) {
            super(context, resource, strings);
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = super.getView(position, convertView, parent);
            TextView textView = view.findViewById(R.id.textview);
            if (position == positionYouWantToColor) {
                textView.setTextColor(getResources().getColor(R.id.mycolor));
            } else {
                textView.setTextColor(getResources().getColor(R.id.black));
            }
            return view;
        }
    }
    

    【讨论】:

    • 我收到错误,String[] 无法转换为 List
    • 我选择了一个构造函数,因为在您的问题中没有指定变量“rez”是什么。但是您可以更改构造函数。见编辑
    【解决方案2】:

    在您的布局文件夹中,您有一个名为 row.xml 的布局。在那里你会有一个TextView,你所要做的就是添加:

         android:color="@color/your-color"
    

    到那个 xml 元素。

    【讨论】:

    • 是的,我知道,但不是所有的行都应该是相同的颜色。我需要你 3 种不同的颜色。
    • 哦,你需要创建一个自定义适配器
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多