列出右侧的导航(微调器)

时间:2013-09-10 15:39:11

标签: java android android-actionbar actionbarsherlock android-spinner

我已经尝试了很多教程,但我找不到能告诉我如何在动作栏(sherlock版本)中创建微调器的文章,但在右侧。

有办法吗?我是否需要创建其他视图和适配器?我只是想知道在右侧创建旋转器的简单方法,没有别的,只是它。

1 个答案:

答案 0 :(得分:5)

您需要为包含微调器的视图创建自定义布局。充气并将其放在动作栏上,你很高兴。

这里有一些示例代码(这是您在活动中执行的操作,以初始化并将布局放在操作栏上):

    LayoutInflater inflater = (LayoutInflater) getSupportActionBar().getThemedContext().getSystemService(LAYOUT_INFLATER_SERVICE);

    final View spinnerView = inflater.inflate(R.layout.layout_spinner, null);
    Spinner spinner = (Spinner) spinnerView.findViewById(R.id.my_spinner);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.spinner_items_array, R.layout.spinner_item);
    adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
    spinner.setAdapter(adapter);

    spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            // Do whatever you want with your selected item. You can get it as: parent.getItemAtPosition(position); 
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {}
    });

    getSupportActionBar().setIcon(getResources().getDrawable(R.drawable.ic_actionbar_logo));//set your actionbar logo
    getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_TITLE );

    LayoutParams layoutParams = new ActionBar.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
    layoutParams.gravity = Gravity.RIGHT; // set your layout's gravity to 'right'
    getSupportActionBar().setCustomView(spinnerView, layoutParams); //place your layout on the actionbar

您的布局应如下所示(layout_spinner.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<Spinner
    style="@style/Widget.Sherlock.Light.Spinner.DropDown.ActionBar"
    android:id="@+id/my_spinner"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="right" />

</LinearLayout>

您的数组存储在res文件夹中(spinner_items_array):

    <string-array name="spinner_items_array">
        <item>Item1</item>
        <item>Item2</item>
    </string-array>

微调器自定义项(spinner_item.xml):

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/cab_spinner_item"
    style="?android:attr/spinnerItemStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ellipsize="marquee"
    android:singleLine="true"
    android:textAlignment="inherit"
    android:textColor="@android:color/white" /> <!-- Set whatever color you want for the text -->

最后是下拉列表项(spinner_dropdown_item.xml):

<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    style="?android:attr/spinnerDropDownItemStyle"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:ellipsize="marquee"
    android:singleLine="true"
    android:textAlignment="inherit"
    android:textColor="@android:color/white" />

我希望这个答案会对你有所帮助!! 祝你好运!