如何更改列表视图中列表项的颜色

发布于 2024-12-08 16:17:11 字数 929 浏览 0 评论 0原文

我在 Activity 类中使​​用 ListView。我没有为 ListView 使用任何 XML 代码。现在我无法更改列表视图中文本项的颜色。

我更改了 ListView 的背景颜色,但无法更改列表项的颜色。我从互联网上获得了一些链接,但无法弄清楚。谁能帮我用一些代码来做到这一点?

我的 Activity 类如下所示:

ListView listView;
        // Create an array of Strings, that will be put to our ListActivity
        String[] names = new String[] { "India", "Malaysia" };
        TextView tv = new TextView(getApplicationContext());
        tv.setText("Select Country");
        tv.setTextColor(012);

        listView = getListView();
        listView.addHeaderView(tv);

        listView.setCacheColorHint(Color.rgb(36, 33, 32));
        listView.setBackgroundColor(Color.rgb(225, 243, 253));
        this.setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,names));

    }

上面,tv.setTextColor 设置为列表项的标题。它不起作用,因为它要求我传递一个整数值。我可以为颜色传递什么整数值? 任何人都可以建议一些用于更改列表项颜色的代码吗?

I am using a ListView in an Activity class. I'm not using any XML code for the ListView. Now I am unable to change the color of the Text item in the listView.

I changed the background color of the ListView but am unable to change the color of the list items. I got some links from the internet but am not able to figure it out. Can anyone help me to do that with some code?

My Activity class looks like this:

ListView listView;
        // Create an array of Strings, that will be put to our ListActivity
        String[] names = new String[] { "India", "Malaysia" };
        TextView tv = new TextView(getApplicationContext());
        tv.setText("Select Country");
        tv.setTextColor(012);

        listView = getListView();
        listView.addHeaderView(tv);

        listView.setCacheColorHint(Color.rgb(36, 33, 32));
        listView.setBackgroundColor(Color.rgb(225, 243, 253));
        this.setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,names));

    }

Above, tv.setTextColor is set for the heading of the list items. And it is not working as it is asking me to pass an integer value. What integer value can I pass for the Color?
Can any one suggest some code for changing the color of the list items?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

长梦不多时 2024-12-15 16:17:11

对于彩色列表项,您需要对其进行自定义。为此,准备一个 xml 文件:

custom_listitem.xml

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


    <TextView android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:textColor="#FFFFFF"
            android:id="@+id/list_item"
            android:background="#FF0000" <!-- for red color -->
            />    

</LinearLayout>

现在您必须在适配器中使用它,例如 -

listView.setListAdapter(new ArrayAdapter<String>(this,R.layout.custom_item,R.id.list_item,names));

是的,您可以使用 Color.REDColor.YELLOW 等作为默认颜色,我们可以使用 < code>"#3C3C3C"(在 xml 中使用时)或 Color.parseColor("3C3C3C")(以编程方式使用时)用于除默认颜色之外的任何颜色。

For having colored listitem,you need to custom it.For that,prepare an xml file:

custom_listitem.xml

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


    <TextView android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:textColor="#FFFFFF"
            android:id="@+id/list_item"
            android:background="#FF0000" <!-- for red color -->
            />    

</LinearLayout>

Now you have to use this in your adapter like-

listView.setListAdapter(new ArrayAdapter<String>(this,R.layout.custom_item,R.id.list_item,names));

And yes,you can use Color.RED,Color.YELLOW etc.for the default colors,our you can use "#3C3C3C" (while using in xml) or Color.parseColor("3C3C3C") (while using programatically) for any colors other than default colors.

少女的英雄梦 2024-12-15 16:17:11

您需要创建一个 CustomListAdapter。

private class CustomListAdapter extends ArrayAdapter {

    private Context mContext;
    private int id;
    private List <String>items ;

    public CustomListAdapter(Context context, int textViewResourceId , List<String> list ) 
    {
        super(context, textViewResourceId, list);           
        mContext = context;
        id = textViewResourceId;
        items = list ;
    }

    @Override
    public View getView(int position, View v, ViewGroup parent)
    {
        View mView = v ;
        if(mView == null){
            LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            mView = vi.inflate(id, null);
        }

        TextView text = (TextView) mView.findViewById(R.id.textView);

        if(items.get(position) != null )
        {
            text.setTextColor(Color.WHITE);
            text.setText(items.get(position));
            text.setBackgroundColor(Color.RED); 
            int color = Color.argb( 200, 255, 64, 64 );
                text.setBackgroundColor( color );

        }

        return mView;
    }

}

列表项如下所示 (custom_list.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/textView"
    android:textSize="20px" android:paddingTop="10dip" android:paddingBottom="10dip"/>
</LinearLayout>

使用 TextView api 根据您的喜好装饰文本

,您将像这样使用它

listAdapter = new CustomListAdapter(YourActivity.this , R.layout.custom_list , mList);
mListView.setAdapter(listAdapter);

You need to create a CustomListAdapter.

private class CustomListAdapter extends ArrayAdapter {

    private Context mContext;
    private int id;
    private List <String>items ;

    public CustomListAdapter(Context context, int textViewResourceId , List<String> list ) 
    {
        super(context, textViewResourceId, list);           
        mContext = context;
        id = textViewResourceId;
        items = list ;
    }

    @Override
    public View getView(int position, View v, ViewGroup parent)
    {
        View mView = v ;
        if(mView == null){
            LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            mView = vi.inflate(id, null);
        }

        TextView text = (TextView) mView.findViewById(R.id.textView);

        if(items.get(position) != null )
        {
            text.setTextColor(Color.WHITE);
            text.setText(items.get(position));
            text.setBackgroundColor(Color.RED); 
            int color = Color.argb( 200, 255, 64, 64 );
                text.setBackgroundColor( color );

        }

        return mView;
    }

}

The list item looks like this (custom_list.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/textView"
    android:textSize="20px" android:paddingTop="10dip" android:paddingBottom="10dip"/>
</LinearLayout>

Use the TextView api's to decorate your text to your liking

and you will be using it like this

listAdapter = new CustomListAdapter(YourActivity.this , R.layout.custom_list , mList);
mListView.setAdapter(listAdapter);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文