2016-08-01 5 views
1

두 개의 열이있는 격자보기가 있습니다. 표보기 행 항목에 대해 다른 색상을 설정하고 싶습니다. 예를 들어, 네 번째 행 항목마다 4 색을 번갈아 가며 적용하고 싶습니다.android에서 gridview 행 항목에 다른 색상을 적용하는 방법은 무엇입니까?

android:background="@color/dividerColor"/> 
<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1.6" 
    android:padding="8dp"> 
    <GridView 
     android:id="@+id/gridCategory" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:numColumns="2" 
     android:verticalSpacing="10dp" 
     android:horizontalSpacing="10dp" 
     android:stretchMode="columnWidth" 
     android:gravity="center"> 
    </GridView> 
</LinearLayout> 
+1

당신은 위치 – Chol

+1

@Abhilash Harsole에 따라 사용자 정의 어댑터를 할 수있는 당신이이 솔루션을 시도했다가 HTTP : //stackoverflow.com/questions/18961709/how-to-set-alternate-row- color-in-a-gridview-android? rq = 1 – PN10

답변

1

이것은 도움이 될만한 시도입니다.

class MyListAdapter extends BaseAdapter{ 

    @Override 
    public int getCount() { 
     return mList.size(); 
    } 

    @Override 
    public News getItem(int position) { 
     return mList.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     if(convertView==null){ 
      convertView=View.inflate(mActivity, R.layout.gridview, null); 
      TextView tvContent = (TextView) convertView 
        .findViewById(R.id.iv_pic); 
     } 
     TextView info = getItem(position); 
     info.setText(info.title); 
     //Is to ensure that the two columns 
     int type=position/2; 
     //For each row processing respectively 
     switch(type){ 
      case 0: 
       convertView.setBackgroundColor(Color.parseColor("#ff0000")); 
       break; 
      case 1: 
       convertView.setBackgroundColor(Color.parseColor("#00ff00")); 
       break; 
      case 2: 
       convertView.setBackgroundColor(Color.parseColor("#0000ff")); 
       break; 
      case 3: 
       convertView.setBackgroundColor(Color.parseColor("#000000")); 
       break; 
     } 
     return convertView; 
    } 

} 
+0

각 행 항목 –

+1

메소드 업데이트에 배경색을 적용하고 싶습니다. 다시 시도하십시오. – lanniaoyidingying

+0

덕분에, 그것은 작동했습니다 ... 그리고 만약 내가 각 행 항목에 대해 찍은 카드보기에 대해 동일한 일을해야합니까? –

관련 문제