2013-04-03 88 views
2

我使用的操作欄福爾摩斯與導航模式NAVIGATION_MODE_LIST定製微調適配器操作欄福爾摩斯NAVIGATION_MODE_LIST - dropdownListPreferredItemHeight忽略

問題是,下拉列表視圖看起來受到擠壓,項目的高度顯示爲具有wrap_content而不是固定的高度。 (this is how it looks

按照* abs_themes *,高度應48dip:

<item name="dropdownListPreferredItemHeight">48dp</item> 

如果我使用它正常工作的資源創造了一個非自定義微調適配器,我缺少什麼?這裏一個樣本代碼,我寫道:

MainActivity.java

package com.example.themetest; 

import android.content.Context; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.SpinnerAdapter; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.actionbarsherlock.app.ActionBar; 
import com.actionbarsherlock.app.ActionBar.OnNavigationListener; 
import com.actionbarsherlock.app.SherlockActivity; 

public class MainActivity extends SherlockActivity { 

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

    ActionBar actionBar = getSupportActionBar(); 
    actionBar.setDisplayShowTitleEnabled(false); 
    actionBar.setDisplayHomeAsUpEnabled(true); 
    actionBar.setDisplayUseLogoEnabled(true); 
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST); 

    final String[] choices = {"Android", "iOS", "RIM"}; 
    CustomSpinnerAdapter customSpinnerAdapter = new CustomSpinnerAdapter(MainActivity.this, android.R.id.text1, choices); 
    actionBar.setListNavigationCallbacks(customSpinnerAdapter, new OnNavigationListener() { 
     @Override 
     public boolean onNavigationItemSelected(int itemPosition, long itemId) { 
      Toast.makeText(MainActivity.this, choices[itemPosition], Toast.LENGTH_SHORT).show(); 
      return false; 
     } 
    }); 

//  this works properly 
//  actionBar.setListNavigationCallbacks(ArrayAdapter.createFromResource(this,  R.array.sections, R.layout.sherlock_spinner_dropdown_item), 
//    new OnNavigationListener() { 
//     public boolean onNavigationItemSelected(int itemPosition, long itemId) { 
//      return false; 
//     } 
//  }); 
} 

private class CustomSpinnerAdapter extends ArrayAdapter<String> implements SpinnerAdapter { 
    private LayoutInflater layoutInflater; 

    public CustomSpinnerAdapter(Context context, int textViewResourceId, String[] values) { 
     super(context, textViewResourceId, values); 
     this.layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

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

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

    private View initView(int position, View convertView) { 
     if (convertView == null) { 
      convertView = layoutInflater.inflate(R.layout.sherlock_spinner_dropdown_item, null); 
     } 
     ((TextView) convertView.findViewById(android.R.id.text1)).setText(getItem(position)); 
     return convertView; 
    } 
} 

} 

styles.xml

<resources> 
    <style name="Theme.AppTheme" parent="Theme.Sherlock.Light" /> 
</resources> 

的manifest.xml

<uses-sdk 
    android:minSdkVersion="8" 
    android:targetSdkVersion="17" /> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/Theme.AppTheme" > 
    <activity 
     android:name="com.example.themetest.MainActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 

+0

是什麼'AppTheme'從延長? – 2013-04-03 22:12:56

+0

我通過創建一個類'public class CustomCursorAdaptor extends SimpleCursorAdapter'來做同樣的事情,爲什麼你要使用'CustomSpinnerAdaptor'? – Mangusto 2013-04-03 22:16:27

+0

@JakeWharton Theme.Sherlock.Light – gwa 2013-04-03 22:19:23

回答

6

你不通過parentinflate()方法。這應該是高度被忽略的原因。

layoutInflater.inflate(R.layout.sherlock_spinner_dropdown_item, parent, false); 

代替

layoutInflater.inflate(R.layout.sherlock_spinner_dropdown_item, null); 

實施getView()getDropDownView()這樣的:

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    if (convertView == null) { 
     convertView = layoutInflater.inflate(
       R.layout.sherlock_spinner_item, parent, false); 
    } 
    ((TextView) convertView.findViewById(android.R.id.text1)) 
      .setText(getItem(position)); 
    return convertView; 
} 

@Override 
public View getDropDownView(int position, View convertView, ViewGroup parent) { 
    if (convertView == null) { 
     convertView = layoutInflater.inflate(
       R.layout.sherlock_spinner_dropdown_item, parent, false); 
    } 
    ((TextView) convertView.findViewById(android.R.id.text1)) 
      .setText(getItem(position)); 
    return convertView; 
} 
+1

f *** ...........! – gwa 2013-04-03 23:16:49

+4

你也只實現'getView'而不是'getDropDownView'。默認情況下,列表導航適配器使用兩個視圖。一個用於顯示在操作欄中的當前所選視圖和另一個(彈出窗口中引用的下拉視圖)。 – 2013-04-04 01:31:11

+0

謝謝,我完全錯過了這裏。我將它添加到答案中。 – 2013-04-04 17:08:41

相關問題