【问题标题】:Any possibility to centre the title within a toolbar? [duplicate]有没有可能在工具栏中将标题居中? [复制]
【发布时间】:2018-11-28 13:03:53
【问题描述】:

我正在使用 Jetpack 开发一个用 Kotlin 编写的小型 Android 应用。 我面临一个小问题,我想将我的工具栏标题居中,但我没有设法弄清楚如何。我看到有些人建议使用自定义工具栏布局,但这样做对我来说是不可能的,因为我希望我的第一页命名为“Grateful”,第二页命名为“Details”而没有任何类似的代码

toolbar.setTitle("Details")

我已经尝试在工具栏布局甚至 appbar 上设置 layout_gravity 和重力以及所有这些东西。但没有任何成功。

这是我的 main_Activity 的实际布局

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

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay"
        app:elevation="0dp">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/primary"
            android:theme="@style/AppTheme.PopupOverlay"
            app:layout_scrollFlags="scroll|snap"
            app:titleTextAppearance="@style/TextAppearanceH5"/>

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

    <fragment
        android:id="@+id/main_notes_nav_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/main_nav_notes"/>

</LinearLayout>

我正在使用 Google 提供的导航库及其 Jetpack 开发套件。

谢谢!

【问题讨论】:

  • 检查我的评论并将其替换为您的

标签: android kotlin fragment toolbar


【解决方案1】:

在您的XML 中,您可以使用自定义toolbar

   <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay">

        <RelativeLayout
            android:id="@+id/toolbarlin"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/toolbarTittle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:maxWidth="150dp"
                android:singleLine="true"
                android:text="@string/app_name"
                android:textColor="@color/white" />

        </RelativeLayout>

    </android.support.v7.widget.Toolbar>

别忘了把你的活动主题改成parent="Theme.AppCompat.Light.NoActionBar"

【讨论】:

  • 这适用于我希望标题为“感恩”的第一页。但在第二页上,感激之情留下了。而且我无法访问我的工具栏,因为它在我的 mainActivity 中,如果我想更改它,它来自一个片段。其实有了导航库,就不需要定义android:text。我的意思是您不必对其进行硬编码,因为它是动态更改的并使用我的导航图中定义的标签。
  • 您可以控制为整个应用或每个活动使用 1 个主工具栏,以拥有由您决定的不同工具栏
  • 尝试创建新的 XML 布局,将工具栏放入其中,然后使用
  • 如果您的应用程序有 1 个主要活动并且您在活动顶部替换片段,您可以在每次访问新页面时发送广播以更改标题,或者制作静态方法并从任何地方调用它我不建议在视图中使用静态
  • 工具栏工具栏 = binding.appBar.findViewById(R.id.toolbar); setSupportActionBar(工具栏); toolbarTittle = binding.appBar.findViewById(R.id.toolbarTittle);而不是使用这种方法 public void setTittle(String tittle) { toolbarTittle.setText(tittle); } 你可以从任何地方调用
猜你喜欢
  • 2016-02-20
  • 2023-03-28
  • 1970-01-01
  • 2015-06-09
  • 1970-01-01
  • 2020-11-15
  • 1970-01-01
  • 1970-01-01
  • 2016-03-06
相关资源
最近更新 更多