android - 如何在android中的工具栏添加 Action 搜索图标

标签 android android-studio android-toolbar

我正在 Android studio 1.1 中工作,我试图在我的工具栏中包含一个搜索选项,该选项搜索显示在下拉菜单中,而不是像操作图标一样显示在工具栏中。我需要将图标搜索放在右侧工具栏中,靠近菜单选项的三点。

这是我的 xml Activity 代码

<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"
    android:orientation="vertical"
    tools:context="com.example.xxxxx.myapplication.MainActivity" >

    <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:minHeight="?android:attr/actionBarSize"
        android:background="#ff7fb4ff"

        />

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawerLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffffffff">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="1">

    </LinearLayout>

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_gravity="left" >

            <ListView
                android:id="@+id/mimenu"
                android:layout_width="match_parent"
                android:layout_height="match_parent"

                android:divider="#eee"
                android:dividerHeight="1dp"
                android:background="#ffa6d0ff" />

    </RelativeLayout>

</android.support.v4.widget.DrawerLayout>

</LinearLayout>

在我的样式 xml 中

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

        <item name="colorPrimary">#005500</item>

        <item name="android:textColorPrimary">#FFFFFF</item>

        <item name="android:textColorSecondary">#FFFFFF</item>



    </style>

java类

public class MainActivity extends ActionBarActivity {

    private CharSequence mTitle;

    private SearchView mSearchView;
    DrawerLayout drawerLayout;
    ArrayAdapter<CharSequence> navigationDrawerAdapter;
    ListView leftDrawerList;
    String[] leftSliderData = new String[]{"uno","dos","tres"};
    ActionBarDrawerToggle drawerToggle;

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

        leftDrawerList = (ListView)findViewById(R.id.mimenu);
        drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
        navigationDrawerAdapter= new ArrayAdapter<CharSequence>( MainActivity.this, android.R.layout.simple_list_item_1, leftSliderData);
        leftDrawerList.setAdapter(navigationDrawerAdapter);

        Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);

        toolbar.setNavigationIcon(R.drawable.ic_drawer);
        //Toolbar will now take on default Action Bar characteristics


        setSupportActionBar(toolbar);

        drawerToggle = new ActionBarDrawerToggle(
                this,
                drawerLayout,
                toolbar,
                R.string.drawer_open,
                R.string.drawer_close) {

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);

            }

            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);

            }
        };
        drawerLayout.setDrawerListener(drawerToggle);

        mTitle = getTitle();

        getSupportActionBar().setDisplayShowTitleEnabled(false);


    }



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

        return true;

    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

菜单代码

<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=".MainActivity"
    android:id="@+id/menuMain"
    >


    <item android:id="@+id/search"
        android:title="@string/search_title"
        android:icon="@drawable/ic_search"

        android:showAsAction="ifRoom"
         />

</menu>

最佳答案

在菜单代码中进行以下更改:android:showAsAction="always"

关于android - 如何在android中的工具栏添加 Action 搜索图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30357802/

相关文章:

android - getContactsFromFirebase() 方法返回一个空列表

android - 我可以从 android studio 在 Windows 11 中安装 Android 应用程序吗?

Android 工具栏标题样式

android - 在 appcompat-v7 的工具栏中删除标题

Android Google play : new app, 使用以前的 obb。不工作

android - 以离线模式加载网站

Android Studio - 如何阻止所有模块构建所有内容?

android - 使用 GPU 调试器 (GAPID) android studio

android - 膨胀类 android.support.v7.widget.Toolbar 时出错

android - 谷歌日历事件链接未打开谷歌日历应用事件创建详细信息