2

我正在使用带有导航模式NAVIGATION_MODE_LIST的Action Bar Sherlock 和自定义微调器适配器

问题是下拉列表视图看起来被挤压,项目的高度看起来像是有 wrap_content 而不是倾斜的固定高度。(这就是它的样子

根据 *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;
    }
}

}

样式.xml

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

清单.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>

4

1 回答 1

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;
}
于 2013-04-03T23:07:27.483 回答