Android 如何通过Styles.XML集中自定义操作栏的标题文本
我刚刚从styles.xml类中创建了一个自定义操作栏,它以我想要的方式显示。我已经从清单文件中激活了它Android 如何通过Styles.XML集中自定义操作栏的标题文本,android,layout,android-styles,Android,Layout,Android Styles,我刚刚从styles.xml类中创建了一个自定义操作栏,它以我想要的方式显示。我已经从清单文件中激活了它 <style name="customStyle" parent="@style/Theme.AppCompat.Light.NoActionBar"> <item name="actionBarStyle">@style/customActionBarStyle</item> <item name="android:
<style name="customStyle" parent="@style/Theme.AppCompat.Light.NoActionBar">
<item name="actionBarStyle">@style/customActionBarStyle</item>
<item name="android:textColorPrimary">@color/titletextcolor</item>
</style>
<style name="customActionBarStyle" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="background">@drawable/custom</item>
</style>
@样式/自定义操作栏样式
@颜色/titletextcolor
@可拉伸/定制
但是,我正试图找到一种方法来集中styles.xml文件中操作栏上的标题文本。请任何人帮我怎么做
我试着用这个:center
。但没有效果
我在网上看到的大多数建议都需要单独创建工具栏布局。但我已经做到了,这不是我想要的,因为它需要更多的代码
谢谢。要在ABS中有一个居中标题(如果您希望在默认的
操作栏中有此标题,只需删除方法名称中的“支持”),您可以执行以下操作:
在活动中,在onCreate
()方法中:
abs\u布局
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="my Title"
android:textColor="#ffffff"
android:id="@+id/mytext"
android:textSize="18sp" />
</LinearLayout>
要在ABS中有一个居中标题(如果您想在默认的ActionBar
,只需删除方法名称中的“support”),您可以这样做:
在活动中,在onCreate
()方法中:
abs\u布局
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="my Title"
android:textColor="#ffffff"
android:id="@+id/mytext"
android:textSize="18sp" />
</LinearLayout>
据我所知,没有容易的办法。默认情况下,标题遵循材料设计指南,并且应该与页面文本对齐,这就是为什么标题位于左侧。但总有一位设计师认为Android是iOS,并希望将文本置于中心位置。所以,如果这真的是你想要做的,你就得付出艰苦的努力。我现在刚刚试过,但那不起作用,不幸的是,它集中了其他东西。顺便说一下,谢谢。据我所知,没有简单的办法。默认情况下,标题遵循材料设计指南,并且应该与页面文本对齐,这就是为什么标题位于左侧。但总有一位设计师认为Android是iOS,并希望将文本置于中心位置。所以,如果这真的是你想要做的,你就得付出艰苦的努力。我现在刚刚试过,但那不起作用,不幸的是,它集中了其他东西。顺便说一下,谢谢。
getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.yourimage));