List navigation (spinner) on the right side

二次信任 提交于 2020-01-02 09:27:12

问题


I have tried so many tutorials, but I could not find article that would tell me how to create spinner in action bar (sherlock version), but on the right side.

Is there a way to do it? Do I need to create additional views and adapters? I just want to know easy way to create that spinner on the right side, nothing else, just it.


回答1:


You'll need to create a custom layout for the view containing the spinner. Inflate and place it on the action bar and you're good to go.

Here you have some sample code for this(this is what you do inside your activity to initialize and place your layout on the action bar):

    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

Your layout should look something like this (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>

Your array stored in a res folder (spinner_items_array):

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

The spinner custom item (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 -->

And finally the drop-down list item (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" />

I hope this answer will help you!! Good luck!



来源:https://stackoverflow.com/questions/18723269/list-navigation-spinner-on-the-right-side

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!