右侧列表导航(离心器) [英] List navigation (spinner) on the right side
问题描述
我已经试过很多教程,但我找不到一篇文章会告诉我如何创建微调的动作栏(夏洛克版),但在右侧。
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.
推荐答案
您需要创建一个自定义布局包含微调的观点。充气并将其放置在操作栏,你是好去。
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.
在这里你有一些样本code本(这是你做了什么你的活动里面来初始化,并把你的布局上的操作栏):
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
您的布局应是这个样子(layout_spinner.xml):
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>
您的阵列存储在res文件夹(spinner_items_array):
Your array stored in a res folder (spinner_items_array):
<string-array name="spinner_items_array">
<item>Item1</item>
<item>Item2</item>
</string-array>
微调器自定义项目(spinner_item.xml):
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 -->
和最后的下拉列表项(spinner_dropdown_item.xml):
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!
这篇关于右侧列表导航(离心器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!