【问题标题】:How to set title of fragment\activity to the toolbar, project template [duplicate]如何将片段\活动的标题设置为工具栏,项目模板[重复]
【发布时间】:2020-10-08 17:25:00
【问题描述】:

我使用 Project Template 中的 Navigation Drawer 创建了新项目,我想将工具栏的标题设置在中心,如 this from this

【问题讨论】:

标签: android android-toolbar android-project-template


【解决方案1】:

试试这个

<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>

在你的活动中

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

【讨论】:

  • 真的是 support.v7.widget.Toolbar 吗?使用 androidx 包或材料组件库。
  • 我的错,我从一个非常古老的项目中得到它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-26
  • 2015-01-17
  • 1970-01-01
  • 1970-01-01
  • 2023-04-10
相关资源
最近更新 更多