android 工具栏位置,Android-如何在工具栏中居中放置标题(TextView)?

在工具栏的左侧,我有一个徽标,我想将TextView放在同一工具栏的中央,以用作标题.由于某些原因,标题没有居中,而是位于右侧.这是我的尝试:

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context=".Register">

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

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="horizontal">

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

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

...

码:

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

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.可以将其删除,并且仍然可以使用.

标签:android,android-toolbar,android-layout,android-activity

来源: https://codeday.me/bug/20191012/1899877.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值