【问题标题】:How to set Title in center of Toolbar in fragment如何在片段的工具栏中心设置标题
【发布时间】:2020-11-05 14:43:24
【问题描述】:

如何在片段的工具栏中心设置标题。

我尝试了各种方法,但都没有奏效。 我想用粗体将标题设置在中心。

我想用粗体将这些标题设置在中心

【问题讨论】:

  • 你不应该在这种情况下使用 layout_constraintVertical_bias,而是在视图项之间使用约束。关于你的问题,这不清楚你想要什么?这里没有标签栏?或者您想将哪个视图 ID 居中?
  • 在片段中分享您的工具栏布局。
  • 如何在片段内共享工具栏布局

标签: android android-layout android-fragments


【解决方案1】:

你可以像这样把text view放在toolbar里面:

<androidx.appcompat.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:layout_constraintTop_toTopOf="parent" >
        <TextView
            android:id="@+id/title"
            android:text="Title"
            android:textSize="20dp"
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </androidx.appcompat.widget.Toolbar>

如果您想将 text view 文本设置为您的应用标题,请使用:

TextView t = (TextView) findViewById(R.id.title) ;
t.setText(getTitle()) ;

结果:

【讨论】:

  • 在我的情况下不起作用。应用您的代码后。它显示了两个标题
  • @Buddy 因为您必须删除原始标题,例如使用 setTitle("") ; 或其他方式,如果您使用工具栏而不是操作栏。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多