【问题标题】:Change ListView layout background color depend on special condition更改 ListView 布局背景颜色取决于特殊条件
【发布时间】:2017-05-03 01:32:06
【问题描述】:

我在Fragment 中创建ListView 并使用SimpleAdapter 设置布局和值。

现在我想根据为ListView 设置的文本更改在ListView 中使用的布局中LinearLayout 的背景。
像这样的:

这是我的布局 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>

【问题讨论】:

  • 在您的适配器中,检查位置并更改布局颜色。比如,如果位置==0 Color.RED,位置==1 COLOR.BLUE
  • @Divyesh 谢谢,但我想根据 textView 中的文本进行更改。但是你所说的改变它取决于位置。
  • 然后使用每个位置的文本值进行更改。

标签: android android-layout listview android-studio


【解决方案1】:
  1. 您需要通过扩展 SimpleAdapter 类来自定义 SimpleAdapter 并覆盖其所有必需的方法。

  2. 在getView()方法下需要编写如下代码

  3. 确保为所有设置了 ID,以便您可以轻松访问 java 代码。

  4. 现在从 textview 的文本中获取文本(颜色)(如“Green”)并设置 LinearLayout 的背景颜色(如 LinearLayout_Object.setBackgroundColor(Color.parseColor("green"));)

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

【讨论】:

  • 谢谢。请您写一个简单的简短示例。
  • TextView 颜色 = (TextView) convertView.findViewById(R.id.colorName); LinearLayout viewForColor = (LinearLayout) convertView.findViewById(R.id.linerLayoutView); color.setText(list.get(position).getColorName()); viewForColor.setBackgroundColor(Color.parseColor(color.getText().toString())); //或者viewForColor.setBackgroundColor(list.get(position).getColorName());
【解决方案2】:

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

例如:

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

【讨论】:

    【解决方案3】:

    在我的adapter 我是这样做的:

    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 中的文本返回所需颜色的方法

    【讨论】:

      【解决方案4】:

      你可以尝试喜欢这个希望这可以帮助你..

        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"));
          }
      

      【讨论】:

      • 什么是yourmodel
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-10
      • 2016-01-11
      • 2018-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多