【问题标题】:Andriod material toolbar isn't showing subtitleAndroid材料工具栏不显示字幕
【发布时间】:2022-08-04 19:26:36
【问题描述】:

我有一个屏幕,我必须在标题中显示屏幕名称,在副标题中显示公司名称。但问题是字幕没有显示在虚拟设备上,但它出现在 xml 编辑器中。

设备屏幕:

XML 编辑器的屏幕:

我像这样以编程方式设置字幕:

when(screenName) {
            \"loginFragment\" -> {binding.ablMain.visibility = View.GONE}
            \"routesFragment\" -> {
                binding.ablMain.visibility = View.VISIBLE
                binding.mtbMain.title = \"Available routes\"
                binding.mtbMain.subtitle = \"Company name\"
            }
            \"ordersListFragment\" -> {
                binding.ablMain.visibility = View.VISIBLE
                binding.mtbMain.title = \"Orders\"
                binding.mtbMain.subtitle = null
            }
            \"orderFragment\" -> {
                binding.ablMain.visibility = View.VISIBLE
                binding.mtbMain.title = \"Order\"
                binding.mtbMain.subtitle = null
            }
        }

但是,使用mtbMain.title 设置标题效果很好,但使用副标题却不行。我试图删除部分代码,我在其中设置了字幕只是为了检查它是否只能与 xml 一起使用,但它也不能。

XML 代码:

<androidx.drawerlayout.widget.DrawerLayout xmlns:tools=\"http://schemas.android.com/tools\"
    android:id=\"@+id/dl_main\"
    android:layout_height=\"match_parent\"
    xmlns:android=\"http://schemas.android.com/apk/res/android\"
    android:layout_width=\"match_parent\"
    xmlns:app=\"http://schemas.android.com/apk/res-auto\">

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width=\"match_parent\"
    android:layout_height=\"match_parent\"
    tools:context=\".MainActivity\">

    <com.google.android.material.appbar.AppBarLayout
        android:id=\"@+id/abl_main\"
        style=\"@style/Widget.MaterialComponents.AppBarLayout.PrimarySurface\"
        android:layout_width=\"match_parent\"
        android:layout_height=\"?attr/actionBarSize\"

        app:layout_constraintStart_toStartOf=\"parent\"
        app:layout_constraintTop_toTopOf=\"parent\">

        <com.google.android.material.appbar.MaterialToolbar
            android:id=\"@+id/mtb_main\"
            style=\"@style/Widget.MaterialComponents.Toolbar.PrimarySurface\"
            android:layout_width=\"match_parent\"
            android:layout_height=\"?attr/actionBarSize\"

            android:background=\"@android:color/transparent\"
            android:elevation=\"0dp\"
            app:navigationIcon=\"@drawable/ic_baseline_menu_24\"
            app:subtitle=\"text\"
            app:subtitleTextAppearance=\"?attr/textAppearanceSubtitle1\"
            app:title=\"Sample text\" />

    </com.google.android.material.appbar.AppBarLayout>...

如何正确设置字幕?

    标签: android android-studio android-toolbar material-components-android


    【解决方案1】:

    安卓工具栏具有内置的设置能力字幕.

    supportActionBar?.setSubtitle(R.string.sub_title_string)
    

    还要确保不要在 Manifest 中针对该 Activity 使用 NoActionBar 主题。

    【讨论】:

      猜你喜欢
      • 2021-05-23
      • 2015-03-29
      • 2017-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多