如何以编程方式在ActionBar工具栏中创建过滤器图标

时间:2017-02-28 11:11:01

标签: java android xml

我只需要创建一个这样的工具栏 Example

有一个过滤器图标,但我想动态添加从数据库中获取的项目,就像我点击项目一样,它将在此菜单中排序fliter icon 我希望这会很清楚 这是我的菜单xml代码

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

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

</menu>

这是我的MainActivity

package abtech.waiteriano.com.actionbartest;

import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class MainActivity extends Activity {

    private MenuItem mSpinnerItem = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    @Override
    public boolean onCreateOptionsMenu( Menu menu )
    {
        mSpinnerItem = menu.findItem( R.id.menu_spinner );
        getMenuInflater().inflate( R.menu.main, menu );
        mSpinnerItem = menu.findItem( R.id.menu_spinner );
        setupSpinner( mSpinnerItem );
        return true;
    }
    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 ) );
        }
    }
}

Error

1 个答案:

答案 0 :(得分:2)

您可以按如下方式获得所需的视图。

<强> activity_main.xml中

<LinearLayout
    xmlns:android=”http://schemas.android.com/apk/res/android”
    xmlns:tools=”http://schemas.android.com/tools”
    android:layout_width=”match_parent”
    android:layout_height=”match_parent”
    tools:context=”.MainActivity”>
    <android.support.v7.widget.Toolbar
        android:id=”@+id/toolbar”
        android:layout_width=”match_parent”
        android:layout_height=”wrap_content”
        android:background=”?attr/colorPrimary”
        android:minHeight=”?attr/actionBarSize” />
</LinearLayout>

创建一个spinner_dropdown_item.xml,其中包含TextView,它表示每个微调器项的视图

<强> spinner_dropdown_item.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<TextView xmlns:android=”http://schemas.android.com/apk/res/android”
    android:id=”@android:id/text1″
    android:layout_width=”match_parent”
    android:layout_height=”wrap_content”
    android:background=”@color/colorPrimary”
    android:gravity=”center_vertical”
    android:minHeight=”?android:attr/listPreferredItemHeightSmall”
    android:paddingLeft=”12dp”
    android:paddingRight=”12dp”
    android:textAppearance=”?android:attr/textAppearanceListItemSmall” />

<强> MainActivity.java

public class MainActivity extends AppCompatActivity {
    private Toolbar toolbar;
    private String[] category={"hello","this","is","me"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        category = getResources().getStringArray(R.array.category);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        toolbar.setLogo(R.drawable.ic_drawer);

        SpinnerAdapter spinnerAdapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.category, R.layout.spinner_dropdown_item);
        Spinner navigationSpinner = new Spinner(getSupportActionBar().getThemedContext());
        navigationSpinner.setAdapter(spinnerAdapter);
        toolbar.addView(navigationSpinner, 0);

        navigationSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this,
                        “you selected: ” + category[position],
                        Toast.LENGTH_SHORT).show();
            }

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

            }
        });

    }
   @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;

    }
}

这是你的menu.xml文件

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.na.quiz.activity.WelcomeActivity">

    <item
        android:id="@+id/action_logout"
        android:orderInCategory="100"
        android:title="@string/action_logout"
        app:showAsAction="always"
        android:icon="@android:drawable/ic_menu_search" />
    <item
        android:title="LIke"
        android:orderInCategory="101"
        android:id="@+id/like"
        app:showAsAction="always"
        android:icon="@drawable/com_facebook_button_like_icon_selected" />

</menu>

我希望它适合你。