2014-03-31 105 views
0

我試圖創建一個自定義的dopdown項目佈局,使項目之間有更多的空間。 本來我有這個代碼:自定義下拉項目佈局在Android中沒有效果

MyListAdapter adapter = new MyListAdapter (this, tmpData); 
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 

這是Android系統內置的佈局:

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/text1" 
    style="?android:attr/spinnerDropDownItemStyle" 
    android:singleLine="true" 
    android:layout_width="match_parent" 
    android:layout_height="?android:attr/dropdownListPreferredItemHeight" 
    android:ellipsize="marquee" /> 

我修改了代碼,以這樣的:

MyListAdapter adapter = new MyListAdapter (this, tmpData); 
adapter.setDropDownViewResource(R.layout.custom_simple_spinner_dropdown_item); 

並用此佈局:

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@android:id/text1" 
       android:singleLine="true" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:ellipsize="marquee" 
       android:layout_marginBottom="24dp" 
       android:layout_marginTop="24dp" 
    /> 

沒有什麼改變。

你能告訴我爲什麼嗎?

+0

您需要使用另一個自定義XML文件來實現此目的。你能顯示那個文件嗎? – Dhaval

+0

你的XML是什麼意思?該佈局僅包含該CheckedTextView。 – Nestor

+0

你不使用「R.layout.custom_simple_spinner_dropdown_item」嗎?似乎你錯過了「R.」。這不是給你錯誤嗎? – Dhaval

回答

0

那麼,事實證明,我設置了2個位置的下拉視圖,第二個使用。 我認爲,當顯示下拉菜單時,它會調用getDropDownView方法,以覆蓋第一個設置。這說得通。

我創建適配器使用自定義dropwdown:

MyListAdapter adapter = new MyListAdapter (this, tmpData); 
adapter.setDropDownViewResource(R.layout.custom_simple_spinner_dropdown_item); 

,我和默認視圖覆蓋它:

@Override 
    public View getDropDownView(int position, View convertView, 
           ViewGroup parent) { 
    LayoutInflater inflater = (LayoutInflater) parent.getContext() 
     .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View itemView = inflater.inflate(android.R.layout.simple_spinner_dropdown_item, null); 

所以,當我把它改成這樣,它的工作原理:

@Override 
    public View getDropDownView(int position, View convertView, 
           ViewGroup parent) { 
    LayoutInflater inflater = (LayoutInflater) parent.getContext() 
     .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View itemView = inflater.inflate(R.layout.custom_simple_spinner_dropdown_item, null);