1

我在操作栏中放置了一个微调器(sherlock),它显示 5 个选项供用户选择,如下所示。到目前为止一切都很好。但我希望微调器在操作栏上正确对齐。微调器的项目也应该居中对齐。知道如何做到这一点吗?

在此处输入图像描述

4

1 回答 1

1

您需要创建自己的微调器适配器。

你可以得到任何你想要的东西,这里有一个例子:

在此处输入图像描述

这是使用自定义微调器的代码:

    Context context = getSupportActionBar().getThemedContext();

    ArrayList<AbMenu> data = new ArrayList<AbMenu>();
    data.add(new AbMenu(isDark ? R.drawable.ic_action_spinner_partiemultidark : R.drawable.ic_action_spinner_partiemulti, getString(R.string.m1), 1));
    data.add(new AbMenu(isDark ? R.drawable.ic_action_spinner_partiemultidark : R.drawable.ic_action_spinner_partiemulti, getString(R.string.m8), 2));
    data.add(new AbMenu(isDark ? R.drawable.ic_action_spinner_partiedark : R.drawable.ic_action_spinner_partie, getString(R.string.s31), 5));
    data.add(new AbMenu(isDark ? R.drawable.ic_action_spinner_savedark : R.drawable.ic_action_spinner_save, getString(R.string.m2), 3));
    data.add(new AbMenu(isDark ? R.drawable.ic_action_spinner_partiehelpdark : R.drawable.ic_action_spinner_partiehelp, getString(R.string.m3), 4));

    AbMenuAdapter adapter = new AbMenuAdapter(context, R.layout.ab_spinner_item, data);

    getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
    getSupportActionBar().setListNavigationCallbacks(adapter, this);

我用来填充适配器的类。

public class AbMenu {
    public int icon;
    public String title;
    public int id;

    public AbMenu() {
        super();
    }

    public AbMenu(int icon, String title, int id) {
        super();
        this.icon = icon;
        this.title = title;
        this.id = id;
    }
}

使用自定义布局的适配器(它是文本视图旁边的图像):

public class AbMenuAdapter extends BaseAdapter implements SpinnerAdapter {

    Context context;
    int layoutResourceId;
    ArrayList<AbMenu> data;
    LayoutInflater inflater;

    public AbMenuAdapter(Context a, int textViewResourceId, ArrayList<AbMenu> data) {
        // super(a, textViewResourceId, data);
        this.data = data;
        inflater = (LayoutInflater) a.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.context = a;
        this.layoutResourceId = textViewResourceId;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            v = inflater.inflate(layoutResourceId, null);
        }
        final AbMenu item = data.get(position);
        if (item != null) {
            ((android.widget.TextView) v.findViewById(R.id.textViewSpinner)).setText(item.title);
            ((ImageView) v.findViewById(R.id.imageViewSpinner)).setImageResource(item.icon);
        }
        return v;
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            v = inflater.inflate(layoutResourceId, null);
        }
        final AbMenu item = data.get(position);
        if (item != null) {
            ((android.widget.TextView) v.findViewById(R.id.textViewSpinner)).setText(item.title);
            ((android.widget.TextView) v.findViewById(R.id.textViewSpinner)).setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
            ((ImageView) v.findViewById(R.id.imageViewSpinner)).setImageResource(item.icon);
        }
        return v;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {

        return 0;
    }
}

和 xml 文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="8dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="8dp"
android:gravity="fill_horizontal" >

<ImageView
    android:id="@+id/imageViewSpinner"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:contentDescription="@string/empty"
    android:padding="4dp"
    android:src="@drawable/ic_launcher" />

<TextView
    android:id="@+id/textViewSpinner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_gravity="center_vertical"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="16dp"
    android:layout_toRightOf="@+id/imageViewSpinner"
    android:text="@string/empty"
    android:ellipsize="end"
    android:singleLine="true" />

</RelativeLayout>
于 2013-04-01T08:37:27.120 回答