我不能对齐一个动作栏的标题,这样它就在中间。 我试着把文字写在动作栏中间。 ,我通过调用函数使用toolbar.xml 它在xml文件中 我试了很多次想找到解决办法,但都没有结果 我使用了很多函数,比如函数setGravity(Gravity.CENTER) 和setGravity(Gravity.CENTER_-HORIZONTAL)和setGravity(Gravity.CENTER_-VERTICAL),我还通过Gravity属性尝试了一些xml代码 See the Java code : private void initToolbar(){ Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar); TextView mToolBarTextView = (TextView) findViewById(R.id.text_view_toolbar_title); setSupportActionBar(mToolbar); if (getSupportActionBar() != null){ getSupportActionBar().setHomeButtonEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayShowTitleEnabled(false); } // mToolbar.setNavigationIcon(R.drawable.btn_back); mToolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v){ onBackPressed(); } }); mToolBarTextView.setText(R.string.title_mc); mToolBarTextView.setTextAppearance(this,R.style.TextBarStyle); // mToolBarTextView.setTypeface(Typeface.DEFAULT_BOLD); // mToolBarTextView.setGravity(Gravity.CENTER_VERTICAL); // mToolBarTextView.setGravity(Gravity.CENTER_HORIZONTAL); // mToolBarTextView.setGravity(Gravity.CENTER); // mToolBarTextView.setGravity(Gravity.CENTER_HORIZONTAL); } // See the my style : <style name="TextBarStyle" parent="TextView.DefaultStyle"> <item name="android:textStyle">bold</item> <item name="android:textColor">#000000</item> </style> // Look at the toolbar xml : <?xml version="1.0" encoding="utf-8"?> <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/toolbar" app:titleTextAppearance="@style/Toolbar.TitleText" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:layout_gravity="center" android:minHeight="?attr/actionBarSize" android:background="?attr/colorPrimary"> <TextView android:id="@+id/text_view_toolbar_title" android:layout_centerInParent="true" android:layout_width="match_parent" android:layout_height="match_parent" android:textColor="@color/text_color" android:textSize="@dimen/tool_bar_text_size" android:gravity="center_horizontal|center_vertical" /> </android.support.v7.widget.Toolbar> // here is activity xml : <?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:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".about_us.AboutUs"> <include layout="@layout/toolbar"/> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="10dp" android:orientation="vertical" android:background="#ffffff" android:padding="10dp"> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="@string/about_us2" android:textSize="19sp"/> <View android:layout_width="match_parent" android:layout_height="2dp" android:layout_marginBottom="10dp" android:layout_marginTop="10dp" android:background="#000"/> <Button android:id="@+id/Button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/about_us4"/> <Button android:id="@+id/Button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/about_us5"/> <Button android:id="@+id/Button3" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/about_us6"/> <View android:layout_width="match_parent" android:layout_height="2dp" android:layout_marginBottom="10dp" android:layout_marginTop="10dp" android:background="#000"/> </LinearLayout> </LinearLayout> 参见Java代码: 私有void initToolbar(){ 工具栏mToolbar=(工具栏)findViewById(R.id.Toolbar); TextView mtoolbertextview=(TextView) findViewById(R.id.text\u view\u toolbar\u title); 设置支持操作栏(mToolbar); 如果(getSupportActionBar()!=null){ getSupportActionBar().setHomeButtonEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayShowTitleEnabled(false); } //mToolbar.setNavigationIcon(R.drawable.btn_back); mToolbar.setNavigationOnClickListener(新视图.OnClickListener(){ @凌驾 公共void onClick(视图v){ onBackPressed(); } }); mtoolbertextview.setText(R.string.title_mc); mtoolbertextview.setTextAppearance(this,R.style.TextBarStyle); //mtoolbertextview.setTypeface(Typeface.DEFAULT_BOLD); //mtoolbertextview.setGravity(重力、重心和垂直); //mtoolbertextview.setGravity(重心、中心和水平); //mtoolbertextview.setGravity(Gravity.CENTER); //mtoolbertextview.setGravity(重心、中心和水平); } //请参见“我的风格”: 大胆的 #000000 //查看工具栏xml: //以下是活动xml:

我不能对齐一个动作栏的标题,这样它就在中间。 我试着把文字写在动作栏中间。 ,我通过调用函数使用toolbar.xml 它在xml文件中 我试了很多次想找到解决办法,但都没有结果 我使用了很多函数,比如函数setGravity(Gravity.CENTER) 和setGravity(Gravity.CENTER_-HORIZONTAL)和setGravity(Gravity.CENTER_-VERTICAL),我还通过Gravity属性尝试了一些xml代码 See the Java code : private void initToolbar(){ Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar); TextView mToolBarTextView = (TextView) findViewById(R.id.text_view_toolbar_title); setSupportActionBar(mToolbar); if (getSupportActionBar() != null){ getSupportActionBar().setHomeButtonEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayShowTitleEnabled(false); } // mToolbar.setNavigationIcon(R.drawable.btn_back); mToolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v){ onBackPressed(); } }); mToolBarTextView.setText(R.string.title_mc); mToolBarTextView.setTextAppearance(this,R.style.TextBarStyle); // mToolBarTextView.setTypeface(Typeface.DEFAULT_BOLD); // mToolBarTextView.setGravity(Gravity.CENTER_VERTICAL); // mToolBarTextView.setGravity(Gravity.CENTER_HORIZONTAL); // mToolBarTextView.setGravity(Gravity.CENTER); // mToolBarTextView.setGravity(Gravity.CENTER_HORIZONTAL); } // See the my style : <style name="TextBarStyle" parent="TextView.DefaultStyle"> <item name="android:textStyle">bold</item> <item name="android:textColor">#000000</item> </style> // Look at the toolbar xml : <?xml version="1.0" encoding="utf-8"?> <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/toolbar" app:titleTextAppearance="@style/Toolbar.TitleText" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:layout_gravity="center" android:minHeight="?attr/actionBarSize" android:background="?attr/colorPrimary"> <TextView android:id="@+id/text_view_toolbar_title" android:layout_centerInParent="true" android:layout_width="match_parent" android:layout_height="match_parent" android:textColor="@color/text_color" android:textSize="@dimen/tool_bar_text_size" android:gravity="center_horizontal|center_vertical" /> </android.support.v7.widget.Toolbar> // here is activity xml : <?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:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".about_us.AboutUs"> <include layout="@layout/toolbar"/> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="10dp" android:orientation="vertical" android:background="#ffffff" android:padding="10dp"> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="@string/about_us2" android:textSize="19sp"/> <View android:layout_width="match_parent" android:layout_height="2dp" android:layout_marginBottom="10dp" android:layout_marginTop="10dp" android:background="#000"/> <Button android:id="@+id/Button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/about_us4"/> <Button android:id="@+id/Button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/about_us5"/> <Button android:id="@+id/Button3" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/about_us6"/> <View android:layout_width="match_parent" android:layout_height="2dp" android:layout_marginBottom="10dp" android:layout_marginTop="10dp" android:background="#000"/> </LinearLayout> </LinearLayout> 参见Java代码: 私有void initToolbar(){ 工具栏mToolbar=(工具栏)findViewById(R.id.Toolbar); TextView mtoolbertextview=(TextView) findViewById(R.id.text\u view\u toolbar\u title); 设置支持操作栏(mToolbar); 如果(getSupportActionBar()!=null){ getSupportActionBar().setHomeButtonEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayShowTitleEnabled(false); } //mToolbar.setNavigationIcon(R.drawable.btn_back); mToolbar.setNavigationOnClickListener(新视图.OnClickListener(){ @凌驾 公共void onClick(视图v){ onBackPressed(); } }); mtoolbertextview.setText(R.string.title_mc); mtoolbertextview.setTextAppearance(this,R.style.TextBarStyle); //mtoolbertextview.setTypeface(Typeface.DEFAULT_BOLD); //mtoolbertextview.setGravity(重力、重心和垂直); //mtoolbertextview.setGravity(重心、中心和水平); //mtoolbertextview.setGravity(Gravity.CENTER); //mtoolbertextview.setGravity(重心、中心和水平); } //请参见“我的风格”: 大胆的 #000000 //查看工具栏xml: //以下是活动xml:,android,xml,android-layout,android-studio,Android,Xml,Android Layout,Android Studio,只需使用相对布局即可我知道这是一种黑客行为。但这会起作用。当导航按钮出现时,工具栏不会提供任何方法将标题精确对齐在工具栏中心。根据文档,我们可以使用“插图”,请参见下面的代码 mToolBarTextView.setPadding(-mToolbar.getContentInsetStartWithNavigation(),0,0,0); 请不要使用任何样式,请从代码中删除这一行并重试 mToolBarTextView.setTextAppearance(this,R.style.Text

只需使用相对布局即可

我知道这是一种黑客行为。但这会起作用。当导航按钮出现时,工具栏不会提供任何方法将标题精确对齐在工具栏中心。根据文档,我们可以使用“插图”,请参见下面的代码

 mToolBarTextView.setPadding(-mToolbar.getContentInsetStartWithNavigation(),0,0,0);
不要使用任何样式,请从代码中删除这一行并重试

 mToolBarTextView.setTextAppearance(this,R.style.TextBarStyle);
根据文件 插图定义了这些组件的最小边距 *并可用于沿已知网格线有效对齐工具栏内容

根据RTL true或false,您可能必须更改左边或右边的填充。可能是。
请尝试让我知道。

在您的样式工作中添加此
center
是否有效?在您的样式工作中添加此
center
是否有效?不幸的是,它不起作用mtoolbertextview.settextearance(this,R.style.TextBarStyle);什么是R.style.TextBarStyle?请参见顶部代码:bold#000000 center@ahmed I更新了答案,您应该删除mtoolbertextview.settextearance(这是R.style.TextBarStyle);代码中的行。我检查了设备及其工作情况。让我知道。不适用于android:supportsRtl=“true”我需要以true的形式工作,仅适用于android:supportsRtl=“false”如何使其与功能一起工作android:supportsRtl=“true”?谢谢你,如果(config.getLayoutDirection()==View.LAYOUT\u DIRECTION\u RTL){mtoolbertextview.setPadding,现在就可以了(+mToolbar.getContentInsertStartWithNavigation(),0,0,0);}else{mtoolbertextView.setPadding(-mToolbar.getContentInsertStartWithNavigation(),0,0);}mtoolbertextView.setPadding(mToolbar.getContentInsertStartWithNavigation(),0,0);用于RTL
 mToolBarTextView.setTextAppearance(this,R.style.TextBarStyle);