我正在使用带有自定义微调器适配器的导航模式 NAVIGATION_MODE_LIST 的Action Bar Sherlock。
问题是下拉列表视图看起来被挤压,项目的高度看起来像是在dip中使用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 :(得分:6)
您没有将parent
传递给inflate()
方法。这应该是忽略高度的原因。
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;
}