【问题标题】:How to Change the font color of a listview [duplicate]如何更改列表视图的字体颜色 [重复]
【发布时间】:2015-02-24 02:06:14
【问题描述】:

这是我的列表视图的代码如何更改列表视图的颜色

public class eat extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_eat);

        String [] Hotels ={"Deer Park","Araliya","Royal Lotus","Kandalama"};
        ListAdapter hoteladapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,Hotels);
        ListView hotellist= (ListView)findViewById(R.id.lv);
        hotellist.setAdapter(hoteladapter);

       // hotellist.setc
    }

【问题讨论】:

  • 只需在 .class 文件中使用 textView.setColor(R.color.your_color); 与您在 listview 中设置的每个 textView

标签: android listview


【解决方案1】:

你可以指定你的列表项布局如下

yourtextviewlayout.xml:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/textView"
    android:textColor="@color/your_color"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>

并将此布局提供给您的ArrayAdapter

ListAdapter hoteladapter = new ArrayAdapter<String>(this,R.layout.yourtextviewlayout,Hotels);

【讨论】:

    【解决方案2】:

    您需要以某种方式访问​​保存您的文本的 TextView 对象。

    你有几个选择:

    • 使用自定义适配器
    • 使用自定义项目布局
    • 快速而肮脏的方式:

      ListAdapter hoteladapter = new ArrayAdapter<String>(this,
              android.R.layout.simple_list_item_1, Hotels) {
          @Override
          public View getView(int position, View convertView, ViewGroup parent) {
              View view = super.getView(position, convertView, parent);
              TextView tv = (TextView) view.findViewById(android.R.id.text1);
              tv.setTextColor(Color.RED);
      
              return view;
          }
      };
      

    【讨论】:

    • 我不推荐后者,因为它不重用行,因此资源效率低。
    • @Marcus 你错了。 ListAdapter 自动重用行。然而,多次调用findViewById 可能会很昂贵,但由于列表仅包含 4 项,因此几乎看不到差异。
    【解决方案3】:

    您必须实现自己的ListAdapter,将您的数据项填充到自定义视图中。网上有很多教程。谷歌搜索会有所帮助。

    【讨论】:

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