Android 根据特殊情况更改ListView布局背景颜色

Android 根据特殊情况更改ListView布局背景颜色,android,android-layout,listview,android-studio,Android,Android Layout,Listview,Android Studio,我在片段中创建列表视图,并使用simpledapter设置布局和值 现在,我想更改我在ListView中使用的布局中的LinearLayout的背景,这取决于为ListView设置的文本 大概是这样的: 这是我的布局XML。 我想用listid更改LinearLayout的颜色 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android

我在
片段中创建
列表视图
,并使用
simpledapter
设置布局和值

现在,我想更改我在
ListView
中使用的布局中的
LinearLayout
的背景,这取决于为
ListView
设置的文本
大概是这样的:

这是我的布局XML。
我想用
list
id更改LinearLayout的颜色

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:background="@drawable/layout_shadow"
    android:layout_height="207dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:background="@color/colorPrimary"
        android:layout_height="193dp"
        android:layout_marginLeft="6dp"
        android:layout_marginRight="6dp"
        android:id="@+id/list">

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <TextView
                android:text="magnitude"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/fValue"
                android:textStyle="bold"
                android:textColor="@android:color/white"
                android:textSize="20sp"
                android:layout_margin="5dp" />
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <TextView
                android:text="region"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/sValue"
                android:textColor="@android:color/white" />
        </LinearLayout>

        <ImageView
            android:layout_width="match_parent"
            app:srcCompat="@drawable/no_image"
            android:id="@+id/mImage"
            android:layout_height="143dp" />
    </LinearLayout>
</RelativeLayout>

您必须检查适配器中的文本条件,然后设置线性布局的背景色

例如:

if(textView.getText().toString().equals("red"))
{
      linearLayout.setBackgroundColor(your color)
}

在我的
适配器中
我这样做:

row = (LinearLayout) findViewById(R.id.llListViewRow);

row.setBackgroundColor(rowBgColor(position));
编辑: 如果您想根据TextView更改颜色:

row = (LinearLayout) findViewById(R.id.llListViewRow);
tv = (TextView) findViewById(R.id.tvTextView);

row.setBackgroundColor(rowBgColor(tv.getText()));
其中
rowBgColor()
应该是根据TextView中的文本返回所需颜色的方法

  • 您需要通过扩展SimpleAdapter类来定制SimpleAdapter,并重写其方法所需的所有内容

  • 在getView()方法下,您需要编写以下代码

  • 确保设置了all的ID,以便可以轻松访问java代码

  • 现在从textview的文本(如“绿色”)中获取文本(颜色),并设置为LinearLayout的背景色(如LinearLayout_Object.setBackgroundColor(color.parseColor(“绿色”);)


  • 注意:但要从数组/列表中获取对象/值,您还需要使用“位置”

    您可以尝试这样做,希望这对您有所帮助

      if (yourmodel.get(postion).getTextName().equalsIgnoreCase("RED")) {
            lLayout.setBackgroundColor(Color.parseColor("#FF0000"));
        } else if (yourmodel.get(postion).getTextName().equalsIgnoreCase("GREEN")) {
            lLayout.setBackgroundColor(Color.parseColor("#008000"));
        } else if (yourmodel.get(postion).getTextName().equalsIgnoreCase("YELLOW")) {
            lLayout.setBackgroundColor(Color.parseColor("#FFFF00"));
        } else {
            lLayout.setBackgroundColor(Color.parseColor("#2F00FF"));
        }
    

    在适配器中,检查布局的位置并更改其颜色。比如,如果position==0 Color.RED,position==1 Color。BLUE@Divyesh谢谢,但我想根据文本视图中的文本进行更改。但是你说的改变取决于位置。然后用每个位置的文本值来改变。谢谢。你能写一个简单的例子吗;LinearLayout视图ForColor=(LinearLayout)convertView.findViewById(R.id.linerLayoutView);color.setText(list.get(position.getColorName());setBackgroundColor(Color.parseColor(Color.getText().toString())//或者viewForColor.setBackgroundColor(list.get(position.getColorName());什么是您的
    型号