如何使用一些按钮制作应用栏,并通过导航操作向上按钮

问题描述 投票:1回答:1

我想制作一个自定义工具栏,我可以添加两个或更多按钮来导航其他部分(例如配置文件和通知区域)和当前片段名称。如果我更改片段并转到另一个地方,则向上按钮消失,返回到最后一个地方。现在我尝试这样处理:

appbar.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:appNs="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/colorPrimaryDark"
    android:id="@+id/toolbar"
    android:paddingStart="20dp"
    android:layoutDirection="rtl"
    android:paddingEnd="10dp"
    android:theme="@style/toolbarTheme">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="my app name"
        android:layout_gravity = "center"
        android:textSize="18sp"
        android:fontFamily="@font/main_bold"
        android:id="@+id/toolbar_title" />

    <ImageView
        android:id="@+id/imgProfile"
        android:src="@drawable/ic_profile"
        android:layout_marginStart="15dp"
        android:layout_gravity="end"
        android:layout_width="25dp"
        android:layout_height="25dp"/>


    <ImageView
        android:id="@+id/imgNotification"
        android:src="@drawable/ic_notification"
        android:layout_width="25dp"
        android:layout_gravity="end"
        android:layout_marginStart="15dp"
        android:layout_height="25dp"/>

</androidx.appcompat.widget.Toolbar>

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>

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

    <data>

    </data>

    <androidx.constraintlayout.widget.ConstraintLayout xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">


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

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

            <androidx.core.widget.NestedScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <fragment
                    android:id="@+id/myNavHostFragment"
                    android:name="androidx.navigation.fragment.NavHostFragment"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    app:defaultNavHost="true"
                    app:navGraph="@navigation/navigation" />
            </androidx.core.widget.NestedScrollView>
        </LinearLayout>

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

MainActivity.kt:

 setSupportActionBar(toolbar)
        val navController = findNavController(R.id.myNavHostFragment)
        val appBarConfiguration = AppBarConfiguration(navController.graph)
        toolbar.setupWithNavController(navController, appBarConfiguration)

我如何为定义在appbar.xml中的ImageView设置onClickListener?以及如何使标签片段文本可自定义?例如更改字体的文字大小和文字颜色并...(现在,当我进入一个片段并且用户向上按钮以返回标签片段时,该片段将变得边缘或消失并从工具栏部分中消失)

android toolbar android-architecture-navigation appbar
1个回答
1
投票

如果使用数据绑定,则可以轻松访问所包含视图的子视图。您需要为包含的布局提供id。只需将数据绑定添加到您包含的布局中,然后使用它访问其视图。在您的主要活动中,情况类似于:

binding.toolbar.imgProfile.setOnClickListener{}
© www.soinside.com 2019 - 2024. All rights reserved.