2016-12-17 93 views
2

我在Fragment中創建ListView並使用SimpleAdapter設置佈局和值。更改ListView佈局背景顏色取決於特殊條件

現在我要在ListView中使用的佈局中的LinearLayout的背景依據爲ListView設置的文本來更改背景。
事情是這樣的:

Something Like This

這是我的佈局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

在適配器中,檢查爲了positio並改變佈局的顏色。像,如果位置== 0 Color.RED,位置== 1 COLOR.BLUE –

+0

@Divyesh謝謝,但我想改變它取決於textView中的文本。但是你說什麼改變取決於立場。 –

+0

然後使用每個位置的文本值進行更改。 –

回答

3
  1. 您需要通過擴展SimpleAdapter類來自定義SimpleAdapter並覆蓋所有需要的方法。

  2. 在getView()方法,你需要下面的代碼編寫

  3. 確保您設定的ID來進行,這樣就可以輕鬆地訪問到Java代碼。

  4. 現在從TextView的文本(如 「綠色」)獲取文本(彩色),並在的LinearLayout的背景色設置(如LinearLayout_Object.setBackgroundColor(Color.parseColor( 「綠色」));)

注:但要獲得從數組/列表對象/值,你需要使用「位置」也

+0

謝謝。你能寫一個簡單的例子嗎? –

+1

TextView color =(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()); – Khushvinder

2

您必須檢查適配器中的文字條件,然後設置線性佈局的背景色。

例如:

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

在我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中的文本

1

你可以嘗試喜歡這個希望這可以幫助你..

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

什麼是yourmodel? –