Android ActionBar(ActionBarCompat)Spinner下拉列表?

时间:2012-11-23 10:12:28

标签: android android-actionbar android-actionbar-compat

我目前正在使用ActionBar(ActionBarCompat)项目,我需要对动作栏进行一些澄清。

我正在使用这样的微调器布局,

<Spinner 
        android:id="@+id/SpinnerList"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

我的代码,

public class MainActivity extends ActionBarActivity implements  OnItemSelectedListener{
    private boolean mAlternateTitle = false;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ActionBar bar = getActionBar();
        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);

        Spinner spinner = (Spinner) findViewById(R.id.SpinnerList);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.locations, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(this);
}
}

它在我的android galaxy ace中显示正常的微调器。我需要输出如下, 是那个位置android 2.3。请提出建议。

enter image description here

感谢。

5 个答案:

答案 0 :(得分:9)

试试这段代码。  在menu / main.xml中:

<item
    android:id="@+id/menu_spinner1"
    android:showAsAction="always"
    android:orderInCategory="1"
    android:actionViewClass="android.widget.Spinner" >
活动中的

private MenuItem mSpinnerItem1 = null;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.


    MenuInflater mi=getMenuInflater();
    mi.inflate(R.menu.main, menu);
    mSpinnerItem1 = menu.findItem( R.id.menu_spinner1);
    View view1 = mSpinnerItem1.getActionView();
    if (view1 instanceof Spinner)
    {
        final Spinner spinner = (Spinner) view1;
        spinner.setAdapter(ad1);


        spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });

    }

    return true;
}

答案 1 :(得分:4)

我觉得现在是时候开始通过ActionBarCompat库轻松地从ABS迁移到ABC了!

使用 ActionBarCompat

的微调器(下拉列表)解决方案
private void setupSpinner(MenuItem item) {
    //  item.setVisible(getSupportActionBar().getNavigationMode() == ActionBar.NAVIGATION_MODE_LIST);
        item.setVisible(ab.getNavigationMode() == ActionBar.NAVIGATION_MODE_LIST);

        View view = item.getActionView();
        Context context = ab.getThemedContext(); //to get the declared theme
        if (view instanceof Spinner) {
            Spinner spinner = (Spinner) view;

            ArrayAdapter<CharSequence> listAdapter =ArrayAdapter.createFromResource(context,
                    R.array.spinner_data,
                    R.layout.support_simple_spinner_dropdown_item);
            listAdapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
            spinner.setAdapter(listAdapter);



        }

确保在style.xml中添加了以下内容:

<style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light"></style>

<style name="AppTheme.MyActionBar" parent="@style/Widget.AppCompat.ActionBar">

更重要的是: menu / main.xml应该是:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:yourapp="http://schemas.android.com/apk/res-auto" >


    <item
        android:id="@+id/menu_spinner"
        yourapp:actionViewClass="android.widget.Spinner"
        android:visible="true"
        yourapp:showAsAction="always"/>

    <item
        android:id="@+id/action_contact"
        android:icon="@drawable/ic_action_user"

        yourapp:showAsAction="ifRoom"
        android:title="@string/action_contact"/>
       <item
        android:id="@+id/action_contact2"
        android:icon="@drawable/ic_action_user"

        yourapp:showAsAction="never"
        android:title="@string/aboutus"/>

</menu> 

我们之前使用的FYI ActionBarSherlock方式:

private void setupSpinner(MenuItem item) {
        item.setVisible(getActionBar().getNavigationMode() == ActionBar.NAVIGATION_MODE_LIST);
        View view = item.getActionView();
        if (view instanceof Spinner) {
            Spinner spinner = (Spinner) view;
            spinner.setAdapter(ArrayAdapter.createFromResource(this,
                    R.array.spinner_data,
                    android.R.layout.simple_spinner_dropdown_item));
        }

与ABS ABC相比,您需要将listadapter设置为R.layout.support_simple_spinner_dropdown_item

答案 2 :(得分:3)

尝试更改

android.R.layout.simple_spinner_item 

android.R.layout.simple_list_item_1

答案 3 :(得分:2)

我认为你正在使用ActionBarCompat,它只是一个示例项目,远远不够用。

尝试使用开源项目ActionBarSherlock。我已经在几个项目中使用它,它功能强大且易于使用。

答案 4 :(得分:0)

如果您要通过菜单的xml将微调框添加到操作栏

android:actionViewClass="android.widget.Spinner"

它会像其他菜单操作一样显示在右侧。

如果要使其显示在左侧,则可以执行另一种方法。

screenshot

我正在一个片段中执行此操作(但是您可以通过活动来执行此操作),并使用Toolbar,所以我的代码是:

activity_main.xmlthemepopupTheme对于箭头颜色很重要):

...
<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:subtitleTextColor="@android:color/white"
        app:titleTextColor="@android:color/white" />
...

action_bar_spinner_title.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@android:id/text1"
    style="?attr/spinnerDropDownItemStyle"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:ellipsize="marquee"
    android:singleLine="true"
    android:textColor="@android:color/white" />

action_bar_spinner_list.xml

<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@android:id/text1"
    style="?android:attr/spinnerDropDownItemStyle"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/colorPrimary"
    android:ellipsize="marquee"
    android:singleLine="true"
    android:textColor="@android:color/white" />

内部片段:

private static final String[] arItems = {"Item 1", "Item 2", "Item 3", };

private Toolbar toolbar;
private Spinner actionBarSpinner;

public ActionBarSpinnerFragment() {
}

@Nullable @Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                         @Nullable Bundle savedInstanceState) {

    return inflater.inflate(R.layout.fragment_action_bar_spinner, container, false);
}

@Override
public void onStart() {
    super.onStart();

    final Activity activity = getActivity();
    if(activity != null) {
        ActionBar actionBar = ((AppCompatActivity) activity).getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayShowTitleEnabled(false);

            toolbar = activity.findViewById(R.id.toolbar);
            actionBarSpinner = new Spinner(actionBar.getThemedContext());
            ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<>(activity,
                    R.layout.action_bar_spinner_title, android.R.id.text1, arItems);
            spinnerAdapter.setDropDownViewResource(R.layout.action_bar_spinner_list);
            actionBarSpinner.setAdapter(spinnerAdapter);
            actionBarSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    FragmentManager fragMngr = getFragmentManager();
                    if(fragMngr != null) {
                        FragmentTransaction fragTransact = fragMngr.beginTransaction();
                        fragTransact.replace(R.id.frag_frame,
                                TextViewFragment.newInstance(arItems[position]));
                        fragTransact.commit();

                    } else {
                        Log.w(LOG_TAG, "ActionBarSpinnerFragment: FragmentManager == null");
                    }
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {
                }
            });
            toolbar.addView(actionBarSpinner);
        } else {
            Log.w(LOG_TAG, "ActionBarSpinnerFragment.onStart: actionBar == null");
        }
    } else {
        Log.w(LOG_TAG,  "ActionBarSpinnerFragment.onStart: activity == null");
    }
}

@Override
public void onStop() {
    super.onStop();
    if(toolbar != null && actionBarSpinner != null) {
        toolbar.removeView(actionBarSpinner);
    } else {
        Log.w(LOG_TAG, "ActionBarSpinnerFragment: toolbar == " + toolbar
                + "; actionBarSpinner == " + actionBarSpinner);
    }
}