2013-12-09 32 views
0

我需要爲某些文本ir行創建背景,如this.如何更改代碼以實現此目的?Android:設置列表視圖文本的背景

我是否需要爲列表視圖擴展另一個基礎適配器?如果有,我該如何做?

代碼:

public View getView(int position, View convertView, ViewGroup parent) { 


     View grid; 
     if(convertView==null){ 
     grid = new View(context); 
     grid = layoutInflater.inflate(R.layout.grid_layout, null); 
     }else{ 
     grid = (View)convertView; 
     } 

     ImageView imageView = (ImageView)grid.findViewById(R.id.gridimage); 

     TextView textView = (TextView)grid.findViewById(R.id.gridtext); 
     imageView.setImageResource(mThumbIds[position]); 
     textView.setText(mStringIds[position]); 

     return grid; 
     } 


     public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_categories); 

     lv = (ListView) findViewById(R.id.grid_list_view); //lv is global 
     lv.setAdapter(simpleAdpt); 

     lv.setOnItemClickListener(new OnItemClickListener(){ 

       @Override 
       public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
         long arg3) { 
        Intent sam = new Intent(CategoriesActivity.this, CategoriesActivity.class); 
        startActivity(sam); 
        System.out.println("on item click"); 

       } 
     }); 

     public void makeList(String value) { 

      String prekes_pav = null; 
      int prekes_kiekis = 0; 


      switch (position) { 
       case 0: 
        prekes_pav = "fr_veg"; 
        break; 
       case 1: 
        prekes_pav = "meat_fish"; 
        break; 
       default: 
        prekes_pav = "No name"; 

      } 
      if(listas.listContain("fr_veg")) 
      { 
       ///////// MAKE text background BLUE 
      } 

      listas.createItem(prekes_pav, value, null); 
      simpleAdpt = new SimpleAdapter(this, listas.getList(), android.R.layout.simple_list_item_1, new String[] {prekes_pav}, new int[] {android.R.id.text1}); 
      lv.setAdapter(simpleAdpt); 
     } 

電網的xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:orientation="vertical"> 
<ImageView 
    android:id="@+id/gridimage" 
    android:layout_width="fill_parent" 
    android:layout_height="40dp"/> 
<TextView 
    android:id="@+id/gridtext" 
    android:textColor="#FFFFFF" 
    android:textSize="16dp" 
    android:gravity="center" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"/> 
</LinearLayout> 

網格窗口XML

<GridView 
    android:id="@+id/grid" 
    android:paddingTop="20dp" 
    android:layout_width="fill_parent" 
    android:layout_height="265dp" 
    android:columnWidth="50dp" 
    android:numColumns="4" 
    android:verticalSpacing="15dp" 
    android:horizontalSpacing="10dp" 
    android:stretchMode="columnWidth" 
    android:gravity="center" 
    android:background="@drawable/bg_blue_gradient" 
/> 
<Button 
    android:id="@+id/cat_button_slide" 
    android:layout_height="wrap_content" 
    android:layout_width="60dp" 
    android:layout_gravity="center" 

    android:background="@drawable/tongue_down" 
    android:onClick="selfDestruct" /> 

<ListView android:id="@+id/grid_list_view" 
       android:layout_height="match_parent" 
       android:layout_width="match_parent" 
    /> 
</LinearLayout> 
+0

你想改變你的xml屬性是什麼? –

+0

grid_list_view行的文本 – user3082220

回答

0

沒有理由構建新的適配器。如果我誤解了你正在做的事情,請告訴我,我會盡力幫忙。

有兩種方法可以做到這一點。一個被XML,以及一個被Java:

內部電網window.xml

<ListView android:id="@+id/grid_list_view" 
      android:background="#0000ff" 
      android:layout_height="match_parent" 
      android:layout_width="match_parent" 
/> 

或者通過代碼:

lv = (ListView) findViewById(R.id.grid_list_view); //lv is global 
    lv..setBackgroundColor(Color.parseColor("#0000ff")); 
    lv.setAdapter(simpleAdpt); 
+0

這將改變整個列表視圖的背景。我只需要更改特定的listview項目文本背景。例如,這裏是第一個包含藍色背景的清單項目,2-5個白色背景,第六個藍色背景等等 - > http://i.stack.imgur.com/bX1yM.png ...背景應該改變到位: 如果(listas.listContain( 「fr_veg」)){ /////////使文本背景藍色 } 這可能嗎? :) – user3082220

+0

嗯。也許這個? http://jsharkey.org/blog/2008/08/18/separating-lists-with-headers-in-android-09/ –

1

更改您的grid.xml的,包括對這樣的事情:

<View 
    android:layout_width="match_parent" 
    android:layout_height="1dp" 
    android:background="#BBBBBB" /> 
+0

這將chane文本背景吹圖標(aple,雞等),並且我需要更改紅色箭頭所在的背景(我需要某些列表元素爲藍色背景,另一個應該有白色)http://i.stack.imgur.com/bX1yM.png – user3082220

+0

@ user3082220更新了答案。 – OrhanC1