【问题标题】:How to set the tool bar title to center in android in java [duplicate]如何在java中将工具栏标题设置为在android中居中[重复]
【发布时间】:2018-04-12 10:04:19
【问题描述】:
toolbar = (Toolbar) findViewById(R.id.gen_toolbar_pln);
setSupportActionBar(toolbar);
toolbar.setTitle(R.string.title_General);

我在java中设置了工具栏的标题。它设置在左角,但我需要使这个居中。

如何使这个标题居中?

任何帮助将不胜感激。

【问题讨论】:

  • 使用android提供的标准工具栏是不可能移动标题的,你需要创建我们自己的工具栏,它可以像线性布局一样简单,带有一个带有子元素的重力顶部这是一个以父级为中心的文本视图。

标签: android


【解决方案1】:

您必须将一个 textview 作为子项添加到工具栏

<android.support.v7.widget.Toolbar
            android:id="@+id/my_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Dark">

            <TextView
                android:id="@+id/title_text"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:gravity="center_horizontal" />
        </android.support.v7.widget.Toolbar>

【讨论】:

  • 是,但不是 center_vertical
  • 我的错,已纠正。谢谢@TimCastelijns
猜你喜欢
  • 1970-01-01
  • 2015-06-09
  • 2019-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-09
相关资源
最近更新 更多