如何使用Android中的Navigation Controller组件仅在某些目标片段上更改工具栏?

时间:2019-05-09 02:08:20

标签: android android-architecture-components android-jetpack android-navigation android-architecture-navigation

所以我有默认的工具栏,该工具栏的中心是文本视图。这是此工具栏的xml:

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar_top"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    android:background="@color/action_bar_bkgnd"
    app:theme="@style/ToolBarTheme" >


     <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Toolbar Title"
        android:layout_gravity="center"
        android:id="@+id/toolbar_title" />


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

在主要活动中作为导航主机,我这样设置工具栏

 private fun setupActionBar(navController: NavController) {

        setSupportActionBar(top_toolbar)
        supportActionBar?.setDisplayShowTitleEnabled(false)

        // set up top hierarchy destination
        val appBarConfiguration = AppBarConfiguration(setOf(
            R.id.destination_home,
            R.id.destination_search,
            R.id.destination_user_control,
            R.id.destination_create_event)
        )

        top_toolbar.setupWithNavController(navController,appBarConfiguration)

    }

但是在特定的目标片段中,我想使用具有自定义搜索栏的工具栏来更改该工具栏,xml如下所示:

<androidx.appcompat.widget.Toolbar
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            android:theme="?attr/actionBarTheme"
            android:minHeight="?attr/actionBarSize"
            android:id="@+id/top_toolbar"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            android:elevation="4dp"
            android:contentInsetLeft="0dp"
            android:contentInsetStart="0dp"
            app:contentInsetLeft="0dp"
            app:contentInsetStart="0dp"
            android:contentInsetRight="0dp"
            android:contentInsetEnd="0dp"
            app:contentInsetRight="0dp"
            app:contentInsetEnd="0dp">

        <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="50dp"
                tools:context=".Fragments.Search.SearchFragment"
                android:id="@+id/constraintLayout_toolbar" tools:layout_editor_absoluteY="8dp"
                tools:layout_editor_absoluteX="16dp">


            <View
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:id="@+id/base_searchView_toolbar_main_activity"
                    android:background="@drawable/round_outline_search_view" app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    android:layout_marginEnd="16dp"
                    app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent"
                    android:layout_marginTop="8dp" android:layout_marginBottom="8dp"
                    app:layout_constraintHorizontal_bias="0.0" app:layout_constraintVertical_bias="1.0"
                    android:layout_marginStart="16dp"/>

            <ImageView
                    android:src="@drawable/ic_search"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/imageView_search_icon_main_activity"
                    app:layout_constraintStart_toStartOf="@+id/base_searchView_toolbar_main_activity"
                    android:layout_marginStart="8dp"
                    app:layout_constraintTop_toTopOf="@+id/base_searchView_toolbar_main_activity"
                    app:layout_constraintBottom_toBottomOf="@+id/base_searchView_toolbar_main_activity"/>

            <EditText
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:inputType="textPersonName"
                    android:ems="10"
                    android:id="@+id/editText_search_keyword_main_activity"
                    android:hint="Cari Acara, Ustadz atau Lokasi" android:textSize="12sp"
                    app:layout_constraintStart_toEndOf="@+id/imageView_search_icon_main_activity"
                    android:layout_marginStart="4dp" android:layout_marginEnd="8dp"
                    app:layout_constraintEnd_toEndOf="@+id/base_searchView_toolbar_main_activity"
                    app:layout_constraintTop_toTopOf="@+id/base_searchView_toolbar_main_activity"
                    app:layout_constraintBottom_toBottomOf="@+id/base_searchView_toolbar_main_activity"
                    android:background="@android:color/transparent"
                    android:imeOptions="actionSearch"
                    android:maxLines="1"/>
            <TextView
                    android:text="TextView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    app:layout_constraintEnd_toEndOf="parent"
                    android:layout_marginEnd="8dp" app:layout_constraintStart_toStartOf="constraintLayout_main_activity"
                    android:layout_marginStart="8dp" android:layout_marginTop="8dp"
                    app:layout_constraintTop_toTopOf="parent" android:layout_marginBottom="8dp"
                    app:layout_constraintBottom_toBottomOf="parent" android:id="@+id/destination_label_text_view"/>


        </androidx.constraintlayout.widget.ConstraintLayout>


    </androidx.appcompat.widget.Toolbar>

那么实现这一目标的最佳方法是什么?

0 个答案:

没有答案