如何在工具栏片段中居中放置字符串



我有以下XML部分,它在我的配置文件片段中生成了一个黑色背景的工具栏,并在其中写入了配置文件。虽然我试着将个人资料居中,但似乎什么都不起作用。有什么建议吗?

<fragment
android:id="@+id/navigation_home"
android:name="com.example.myapp_android.ui.home.HomeFragment"
android:label="@string/title_home"
tools:layout="@layout/fragment_home" />

我尝试添加到<fragment android:label_gravity = "center" and android:gravity = "center"
,但他们都没有移动配置文件文本。

工具栏可以有子视图,因此您可以在工具栏中使用TextView

使用此:

<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/toolbar"
>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Toolbar"
android:textSize="20sp"
android:textStyle="bold"/>


</androidx.appcompat.widget.Toolbar>

最新更新