java - 右侧列表导航(微调器)

标签 java android android-actionbar actionbarsherlock android-spinner

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

有办法吗?我需要创建额外的 View 和适配器吗?我只是想知道在右侧创建旋转器的简单方法,仅此而已。

最佳答案

您需要为包含微调器的 View 创建自定义布局。充气并将其放在操作杆上,就可以开始了。

这里有一些示例代码(这是您在 Activity 中初始化并将布局放置在操作栏上的操作):

    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" />

希望这个回答对你有帮助!! 祝你好运!

关于java - 右侧列表导航(微调器),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18723269/

相关文章:

java - 为什么在 Java 中使用 final 实例类变量?

java - Freeswitch channel 异常

android - 如何对浏览器的 Intent 使用react?

android - 使用 RxAndroid MVP Retrofit 修改回调

android - 在操作栏中移动图像按钮

java - 纵向模式下带有图标和文本的 ActionBar

android - ActionBar 和 ViewPager 以及 CursorLoader 可以工作,但也许有更好的方法

java - Spring 启动 : "Scope ' request' is not active for the current thread"in Asynch method

android - 使用带有 HttpURLConnection 的 POST 发送文件

java - Axis2 (1.6.1) 客户端 Web 服务基本身份验证