Android-如何在工具栏中居中放置标题(TextView)? [英] Android - How to center a title (TextView) in a toolbar?
问题描述
在工具栏的左侧,我有一个徽标,我想将TextView放在同一工具栏的中央,以用作我的标题.由于某些原因,标题没有居中,而是位于右侧.这是我的尝试:
On the left of my toolbar I have a logo, and I want to center a TextView in the same toolbar to act as my title. For some reason, the title does not center, and is instead on the right. Here is my attempt:
<?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);
有人知道为什么标题不在工具栏中居中吗?任何帮助都将受到高度赞赏.
Does anybody know why the title does not center in the toolbar? Any help is highly appreciated.
推荐答案
有两种方法可以解决您的问题.最快的方法是如下更改您的TextView.
There are a couple of ways to solve your problem. The quickest one would be to change your TextView as follows.
<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>
这可确保您的文字将覆盖图标旁边的整个空间,并使文字在其中居中.您甚至不需要LinearLayout
.可以将其删除,并且仍然可以使用.
This makes sure that your text would cover the entire space next to the icon and center the text within it. You dont even need a LinearLayout
for that. It can be removed and it'll still work.
这篇关于Android-如何在工具栏中居中放置标题(TextView)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!