簡體   English   中英

如何在Android工具欄中添加動作搜索圖標

[英]how to add an action search icon to toolbar in android

我在Android Studio 1.1中工作,並且試圖在工具欄中包含搜索選項,該選項搜索顯示在下拉菜單中,而不是在工具欄中(如操作圖標)。 我需要在菜單選項三點附近的右側工具欄中放置圖標搜索。

這是我的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"
    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"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM