2012-09-17 155 views
0

我嘗試創建一個微調選擇顏色。顏色應顯示在Spinner標題中。下面是截圖:自定義微調背景

Sceenshot

但正如你所看到的,顏色不填我的9補丁微調背景(頭)。這裏是9補丁背景:

Background

但我不明白爲什麼...
我實現了一個自定義的ArrayAdapter,這裏是代碼:

spinner = (Spinner) findViewById(R.id.spinner); 
ArrayAdapter<String> adapter = new MyArrayAdapter(this, R.layout.spinner_row, R.id.textView1, items); 
spinner.setAdapter(adapter); 

private class MyArrayAdapter extends ArrayAdapter<String>{ 

    public MyArrayAdapter(Context context, int resource, int textViewResourceId, String[] objects) { 
     super(context, resource, textViewResourceId, objects); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     return getCustomViewSmall(position, convertView, parent); 
    } 

    @Override 
    public View getDropDownView(int position, View convertView, ViewGroup parent) { 
     return getCustomView(position, convertView, parent); 
    } 

    public View getCustomView(int position, View convertView, ViewGroup parent) { 
     LayoutInflater inflater = getLayoutInflater(); 
     View row = inflater.inflate(R.layout.spinner_row, parent, false); 
     View v = (View) row.findViewById(R.id.spinnerFrame); 
     v.setBackgroundColor(getColor(position)); 
     return row; 
    } 

    public View getCustomViewSmall(int position, View convertView, ViewGroup parent) { 
     LayoutInflater inflater = getLayoutInflater(); 
     View row = inflater.inflate(R.layout.spinner_row_small, parent, false);  
     row.setBackgroundColor(getColor(position));  
     return row; 
    } 

    private int getColor(int pos) { 
     switch(pos) { 
     case 0: 
      return colors[0]; // Blue 
     case 1: 
      return colors[1]; // Purple 
     case 2: 
      return colors[2]; // Orange 
     case 3: 
      return colors[3]; // Yellow 
     case 4: 
      return colors[4]; // Cyan 
     } 
     return colors[5]; 
    } 
} 

這裏是佈局的代碼:
的spinner_row_small.xml(即用於報頭的佈局):

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/spinnerFrameSmall" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:layout_gravity="center" > 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 

</FrameLayout> 

的spinner_row.xml(佈局爲行):

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/FrameLayout1" 
android:layout_width="match_parent" 
android:layout_height="50dp" 
android:layout_gravity="center" 
android:background="@color/white" > 

<FrameLayout 
    android:id="@+id/spinnerFrame" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_gravity="center" 
    android:layout_marginBottom="1dp" 
    android:layout_marginTop="1dp" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" /> 
</FrameLayout> 
</FrameLayout> 

而且main.xml中:

 <Spinner 
      android:id="@+id/spinner" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@drawable/textview" 
      android:spinnerMode="dropdown"  
      android:popupBackground="@drawable/spinner_background"   
      android:layout_marginBottom="30dp" /> 

我會很高興,如果有人能幫助我。
此致敬禮!

+2

圖像鏈接不良。使用SO自己的上傳功能完整 –

回答

1

我看不出你的代碼有什麼問題,但nine patch image的問題在於,只有被標記的空間會被放大,你將只能得到實際使用中標記的圖像空間。這就是爲什麼你會在所有方面獲得空間(這是沒有標記的)。我認爲九個補丁圖像對於簡單的背景非常有用,沒有太多顏色複雜性

+0

我不知道我是否正確。我修改了我的九段影像,但結果是一樣的: [new nine-patch](https://dl.dropbox.com/u/61662712/textview_spinner2.9.png) 隨着新的9-補丁,顏色必須填滿整個區域。但結果看起來依然如此。這就是爲什麼我認爲問題不在於我的背景圖像。 此致敬禮。 – Namenlos

+0

一次清潔項目n再次運行 – Braj

+0

不,不變。但現在我用屬性「minHeigt」處理它。感謝您的麻煩! – Namenlos