【问题标题】:Change the title of MaterialToolbar into center将 MaterialToolbar 的标题改为 center
【发布时间】:2021-01-11 09:34:13
【问题描述】:

我正在使用导航组件和材质工具栏。 我刚刚设置了带有材质工具栏的导航组件。每当片段更改显示的后退按钮和材质工具栏的标题时,导航组件都会自动更改。

问题是我只想将标题重心更改为中心而不删除那些导航组件工具栏支持。

我该怎么做?

注意:我尝试更改工具栏样式,但似乎不起作用。 并且在工具栏视图中创建一个额外的文本视图不是解决方案,因为我想使用导航组件工具栏支持。

提前致谢。

【问题讨论】:

    标签: android android-architecture-navigation


    【解决方案1】:

    com.google.android.material:material:1.4.0 开始,您可以使用app:titleCentered="true" 将标题居中。

    <com.google.android.material.appbar.MaterialToolbar
                android:id="@+id/materialToolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:title="Toolbar"
                app:titleCentered="true"/>
    

    我希望这会有所帮助。如果符合您的需求,您可以将其标记为已接受。

    【讨论】:

      【解决方案2】:

      您可以使用自定义工具栏

      <androidx.appcompat.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" />
      
      
      <androidx.appcompat.widget.Toolbar/>
      

      它只是视图组的一部分,您可以随意设置 TextView 的样式,因为它只是一个常规的 TextView。因此,在您的活动中,您可以像这样访问标题:

      Toolbar toolbarTop = (Toolbar) findViewById(R.id.toolbar_top);
      TextView mTitle = (TextView) toolbarTop.findViewById(R.id.toolbar_title);
      

      【讨论】:

      • 感谢您回答我的问题,但正如我在问题中提到的,我不想在工具栏中创建一个新的额外视图。
      【解决方案3】:

      作为一种棘手的方式,您可以使用:com.google.android.material.appbar.CollapsingToolbarLayout

              <com.google.android.material.appbar.CollapsingToolbarLayout
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:fitsSystemWindows="true"
                  app:collapsedTitleGravity="center"
                  app:collapsedTitleTextAppearance="?attr/textAppearanceHeadline3"
                  app:expandedTitleGravity="center"
                  app:expandedTitleTextAppearance="?attr/textAppearanceHeadline1"
                  app:layout_scrollFlags="scroll|exitUntilCollapsed">
      
                  <androidx.appcompat.widget.Toolbar
                      android:id="@+id/toolbar"
                      android:layout_width="match_parent"
                      android:layout_height="?attr/actionBarSize"
                      app:contentInsetStart="0dp"
                      app:contentInsetStartWithNavigation="0dp"
                      app:layout_collapseMode="pin"
                      app:navigationIcon="@drawable/ic_back_24dp"
                      app:title="@string/parvaneh_personal_info" />
      
              </com.google.android.material.appbar.CollapsingToolbarLayout>
      

      【讨论】:

      • 感谢您回答我的问题,我已经尝试过您的解决方案,但它不起作用...
      猜你喜欢
      • 2020-08-26
      • 1970-01-01
      • 1970-01-01
      • 2020-11-20
      • 1970-01-01
      • 1970-01-01
      • 2021-04-25
      • 1970-01-01
      • 2022-08-09
      相关资源
      最近更新 更多