2013-09-23 82 views
3

如何在gridview中設置交替行顏色?我已經搜索了很多關於如何設置網格視圖行顏色的教程,但沒有任何關於gridview行顏色的內容。我只有備用行顏色的列表視圖。我需要交替行應該是白色和黑色。在這裏,我包括我的代碼。請幫幫我!!!!!!!!!!如何在Gridview Android中設置備用行顏色?

這裏是Java類]:

public class MainActivity extends Activity { 
    GridView gridView; 

    static final String[] numbers = new String[] { "A", "B", "C", "D", "E", 
      "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", 
      "S", "T", "U", "V", "W", "X", "Y", "Z", "A", "B", "C", "D", "E", 
      "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", 
      "S", "T", "U", "V", "W", "X", "Y", "Z", "A", "B", "C", "D", "E", 
      "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", 
      "S", "T", "U", "V", "W", "X", "Y", "Z", "A", "B", "C", "D", "E", 
      "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", 
      "S", "T", "U", "V", "W", "X", "Y", "Z", "A", "B", "C", "D", "E", 
      "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", 
      "S", "T", "U", "V", "W", "X", "Y", "Z", "A", "B", "C", "D", "E", 
      "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", 
      "S", "T", "U", "V", "W", "X", "Y", "Z", "A", "B", "C", "D", "E", 
      "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", 
      "S", "T", "U", "V", "W", "X", "Y", "Z" }; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     gridView = (GridView) findViewById(R.id.gridView1); 

     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
       android.R.layout.simple_list_item_1, numbers); 
     gridView.setAdapter(adapter); 

    } 
    } 

下面是XML:

<?xml version="1.0" encoding="utf-8"?> 
<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gridView1" 
    android:numColumns="10" 
    android:gravity="center" 
    android:columnWidth="50dp" 
    android:stretchMode="columnWidth" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
</GridView> 

請提供良好的教程或樣品實施例。提前致謝!!!!!!!!!!!

回答

5

創建一個自定義適配器並覆蓋其getView方法:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    gridView = (GridView) findViewById(R.id.gridview1); 

    MyAdapter adapter = new MyAdapter(this, 
      R.layout.item, numbers); 

    gridView.setAdapter(adapter); 

} 

public class MyAdapter extends ArrayAdapter<String> { 

    String[] objects; 
    Context context; 

    public MyAdapter(Context context, int textViewResourceId, String[] objects) { 
     super(context, textViewResourceId, objects); 
     this.context = context; 
     this.objects = objects; 
    } 


    @Override 
    public View getView(int position, android.view.View convertView, android.view.ViewGroup parent) { 
     TextView tv; 
     if (convertView == null) { 
      LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      tv = (TextView)inflater.inflate(R.layout.item,parent,false); 
     } else { 
      tv = (TextView) convertView; 
     } 
     tv.setText(objects[position]); 
     if (position % 2 == 0) 
      tv.setBackgroundColor(Color.BLACK); 
     else 
      tv.setBackgroundColor(Color.WHITE); 

     return tv; 
    } 
} 

item.xml

<?xml version="1.0" encoding="utf-8"?> 

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 

</TextView> 
+0

Mohsen Afshin @它的工作很好先生。感謝您的回答。還有一件事是如何更改備用列顏色? – Palanichamy

+0

它與行相同,但您應該手動設置單元格的行和列(在getView中),然後根據列號更改背面顏色。這是你有一個邏輯來確定每個單元格的行號和列號(字符,在這裏) –

+0

Mohsen Afshin @謝謝先生。 – Palanichamy

0

使用自定義適配器和定製適配器
你將有方法getView

使用此代碼,您getView方法

if(position % (2*columnCount) == 0) 
{ 
// set first color 
} 
else 
{ 
// set alternate row's color 
} 
+0

它會更像'position%columnCount'。 – kcoppock

+0

@ kcoppock亞,但不完全像它應該是這樣的 – Trikaldarshi

+0

高科技KitKat Android @謝謝你的答案先生。 – Palanichamy