【问题标题】:How to set toolbar title in center in android in JAVA [duplicate]如何在JAVA中的android中心设置工具栏标题[重复]
【发布时间】:2016-01-16 02:22:11
【问题描述】:

我知道在中心设置工具栏标题的布局方式。 (在 Toolbar 中有一个 TextView)

我正在寻找同样的 java 方式。 这是我的工具栏布局

<android.support.v7.widget.Toolbar
  android:id="@+id/toolbar"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:background="@color/colorPrimary"
  android:minHeight="?attr/actionBarSize">
</android.support.v7.widget.Toolbar>

这就是我在活动中设置工具栏标题的方式:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("MY ORDERS");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

标题默认左对齐。

有没有办法(不改变布局)使标题居中对齐?

【问题讨论】:

  • 如果您可以查看我已经明确提到的 JAVA 方式的问题。您提到的可能重复在布局文件中提供了一种解决问题的方法
  • @DanielNugent 添加了有问题的工具栏布局。

标签: android


【解决方案1】:

Toolbar 小部件可以有子视图,可用于自定义 Toolbar 的外观。 居中文本的方法是使用 TextView 作为 Toolbar 的子项,然后对其进行自定义。

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:minHeight="?attr/actionBarSize">

    <TextView
        android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="@string/app_name"
        android:textColor="@color/black" />

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

【讨论】:

    猜你喜欢
    • 2019-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多