基于导航视图选择的Android更改工具栏

时间:2016-06-22 10:24:27

标签: android android-toolbar drawerlayout

我有一个名为 MainActivity.java 的活动,在此我实现了基于 DrawerLayout 的导航视图

以下是XML代码

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

        </android.support.design.widget.AppBarLayout>

        <include layout="@layout/content_main" />

    </android.support.design.widget.CoordinatorLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/menu_navigation"/>

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

在导航视图中,我有4个选项。以下是页面的输出

enter image description here

以下是我需要做的事情 1.我需要显示一个包含与我的应用程序相关的文本的图像,而不是单词 MainActivity

  1. 最初加载活动时,我不想在右上角显示搜索图标。

  2. 在导航视图中,当我选择第二个选项时,我需要显示搜索图标,当我选择第三个选项时,我需要显示一个+按钮而不是搜索图标。

  3. 如何通过自定义工具栏来实现上述所有内容。

2 个答案:

答案 0 :(得分:0)

    Here you can do the following

    1. Instead of the word MainActivity i need to show an image which contains the text related to my app.
    You can customise the toolbar by creating a custom toolbar and placing the image which you want to show.

toolbar.xml


    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimaryDark"
    android:minHeight="?attr/actionBarSize"
    app:contentInsetLeft="0dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">
    app:contentInsetStart="0dp">


       <android.support.v7.widget.AppCompatTextView
        android:id="@+id/txt_screens_toolbar_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:singleLine="true"
        android:text="@string/app_name"
        android:textColor="@android:color/white"
        android:textSize="22dp" />

    </android.support.v7.widget.Toolbar>

For 2 & 3 :
Here you can get the reference of the search and plus icon,by which you can hide and show them simultaneously.
getActivity().findByViewId() inside the fragment.

答案 1 :(得分:0)

这会对你有所帮助

创建customtoolbar.xml

 <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar 
xmlns:android="http://schemas.android.com/apk/res/android"
                                   android:id="@+id/toolbar_top"
                                   android:layout_width="match_parent"
                                   android:layout_height="wrap_content"
                                   android:background="@color/colorPrimary"
                                   android:minHeight="?attr/actionBarSize">


    <ImageView
        android:id="@+id/imgLeft"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:src="@mipmap/ic_launcher"
       />



    <ImageView
        android:id="@+id/imgLogo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:visibility="gone"/>

    <ImageView
        android:id="@+id/imgRight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:src="@mipmap/ic_launcher"
        android:text="Right Menu"
        android:visibility="gone"/>


</android.support.v7.widget.Toolbar>

在AppBarLayout标记

下包含此customtoolbar.xml文件

在MainActivity中

NavigationView view = (NavigationView) findViewById(R.id.navigation_view);
        view.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {

                selectDrwerMenu(menuItem);

                return true;
            }
        });

//这里的主要逻辑

 private void selectDrwerMenu(MenuItem menu) {
        switch (menu.getItemId()) {
            case R.id.first:
                //change or add image to toolbar imageview
                break;
            case R.id.second:

                //show toolbar right side image
                break;
            case R.id.third:

                break;
            default:
                mDrawerLayout.closeDrawers();
        }
    }

请将操作栏替换为MainActivity中的工具栏 这个http://mrengineer13.com/how-to-use-toolbar-to-replace-your-actionbar/