如何在 ListView 中添加不同的颜色

发布于 2024-11-10 19:00:49 字数 297 浏览 5 评论 0原文

我想在列表视图的每一行中获得不同的颜色

,但当前仅显示一种颜色

,所以我该怎么做,

ArrayAdapter<String> adapter=new ArrayAdapter<String(this,R.layout.latesthappenings,R.id.LH_Titles,titles);
            setListAdapter(adapter);

我想在每一行中获得不同的颜色,

请告诉我该怎么做,

谢谢

i want to get the different colors in each row of the list view

but current only one color is shown

so how can i do this

ArrayAdapter<String> adapter=new ArrayAdapter<String(this,R.layout.latesthappenings,R.id.LH_Titles,titles);
            setListAdapter(adapter);

i want to different color in each row

please tell how can i do

thanks

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

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

发布评论

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

评论(2

意犹 2024-11-17 19:00:49

您可以使用匿名ArrayAdapter,您必须在getView()

Sudo代码

ArrayAdapter<String> adapter=new ArrayAdapter<String>(
           this,R.layout.main,R.id.textview1,titles){

   @Override
   public View getView(int position, View convertView, ViewGroup parent) {
if(position ==condition){
convertView.setBackgroundResource(R.color.grey);}
else if(second condition){convertView.setBackgroundResource(R.color.something else);}

    return view;};
   };

中编写代码另一种方法是

为什么不为自定义ListView做呢?您可以根据您的要求管理视图。

有一个 自定义 ArrayAdapter

谢谢
迪帕克

You can use anonymous ArrayAdapter you have to write code inside getView()

Sudo code

ArrayAdapter<String> adapter=new ArrayAdapter<String>(
           this,R.layout.main,R.id.textview1,titles){

   @Override
   public View getView(int position, View convertView, ViewGroup parent) {
if(position ==condition){
convertView.setBackgroundResource(R.color.grey);}
else if(second condition){convertView.setBackgroundResource(R.color.something else);}

    return view;};
   };

ANother approach is

Why donot you do for Customized ListView? You can manage view as per your requirement there.

Have a Custom ArrayAdapter

Thanks
Deepak

抱着落日 2024-11-17 19:00:49

在数组中设置所有不同的颜色(列表中所需的)代码,并从该数组中为每个行位置获取颜色代码,并使用 setBackgroundColor() 方法将该背景颜色设置为列表行。

试试这个。

Set all the different colors (that you want in your list) codes in an array and fetch color code from that array for each row position and use setBackgroundColor() method to set that background color to the list row.

Try this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文