Android - 如何使工具栏中的标题 (TextView) 居中?
Android - How to center a title (TextView) in a toolbar?
我的工具栏左侧有一个徽标,我想在同一工具栏中居中放置一个 TextView 作为我的标题。由于某种原因,标题没有居中,而是在右边。这是我的尝试:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Register">
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/sign_up_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:fitsSystemWindows="true"
app:contentInsetLeft="5dp"
app:contentInsetStart="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/toolbar_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:maxHeight="60dp"
android:maxWidth="60dp"
android:scaleType="fitCenter"
android:src="@mipmap/icon" />
<TextView
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Sign Up"
android:textColor="#ffffff"
android:textSize="21dp"
android:textStyle="bold" />
</LinearLayout>
</android.support.v7.widget.Toolbar>
...
</LinearLayout>
代码:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Typeface myCustomFont = Typeface.createFromAsset(getAssets(), "fonts/scss.ttf");
setContentView(R.layout.frag_register_name);
Toolbar toolbar = (Toolbar) findViewById(R.id.sign_up_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#3f9845")));
getSupportActionBar().setDisplayShowTitleEnabled(false);
有人知道为什么标题不在工具栏中心吗?非常感谢任何帮助。
有几种方法可以解决您的问题。最快的方法是按如下方式更改您的 TextView。
<TextView
android:id="@+id/toolbar_title"
android:layout_width="match_parent" // Fill parent
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Sign Up"
android:gravity="center" // Make the text center aligned
android:textColor="#ffffff"
android:textSize="21dp"
android:textStyle="bold" />
</LinearLayout>
这可确保您的文本覆盖图标旁边的整个 space 并使文本居中。你甚至不需要 LinearLayout
。它可以被删除,它仍然有效。
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/title_activity_add_medication"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="@android:color/white"
android:textAllCaps="true"
android:layout_marginEnd="72dp"
android:gravity="center"/>
</android.support.v7.widget.Toolbar>
如果您以 gravity
为中心,那么它应该可以工作。另一种方法是将文本视图的宽度设置为 match_parent 并将 text_alignment
作为中心
我的工具栏左侧有一个徽标,我想在同一工具栏中居中放置一个 TextView 作为我的标题。由于某种原因,标题没有居中,而是在右边。这是我的尝试:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Register">
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/sign_up_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:fitsSystemWindows="true"
app:contentInsetLeft="5dp"
app:contentInsetStart="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/toolbar_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:maxHeight="60dp"
android:maxWidth="60dp"
android:scaleType="fitCenter"
android:src="@mipmap/icon" />
<TextView
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Sign Up"
android:textColor="#ffffff"
android:textSize="21dp"
android:textStyle="bold" />
</LinearLayout>
</android.support.v7.widget.Toolbar>
...
</LinearLayout>
代码:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Typeface myCustomFont = Typeface.createFromAsset(getAssets(), "fonts/scss.ttf");
setContentView(R.layout.frag_register_name);
Toolbar toolbar = (Toolbar) findViewById(R.id.sign_up_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#3f9845")));
getSupportActionBar().setDisplayShowTitleEnabled(false);
有人知道为什么标题不在工具栏中心吗?非常感谢任何帮助。
有几种方法可以解决您的问题。最快的方法是按如下方式更改您的 TextView。
<TextView
android:id="@+id/toolbar_title"
android:layout_width="match_parent" // Fill parent
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Sign Up"
android:gravity="center" // Make the text center aligned
android:textColor="#ffffff"
android:textSize="21dp"
android:textStyle="bold" />
</LinearLayout>
这可确保您的文本覆盖图标旁边的整个 space 并使文本居中。你甚至不需要 LinearLayout
。它可以被删除,它仍然有效。
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/title_activity_add_medication"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="@android:color/white"
android:textAllCaps="true"
android:layout_marginEnd="72dp"
android:gravity="center"/>
</android.support.v7.widget.Toolbar>
如果您以 gravity
为中心,那么它应该可以工作。另一种方法是将文本视图的宽度设置为 match_parent 并将 text_alignment
作为中心