如何通过Styles.XML集中自定义操作栏的标题文本
我刚刚从我的styles.xml类创建了一个自定义操作栏,它显示了我想要的方式。我已经从清单文件中激活了它。如何通过Styles.XML集中自定义操作栏的标题文本
<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>
但是,我试图找到一种方法来从styles.xml文件中在操作栏上标题文本CENTRALIZE。请任何人都可以帮助如何做到这一点。
我试过用这个:<item name="android:layout_gravity">center</item>
。但它没有效果。
我在网上看到的大多数建议都需要单独创建工具栏布局。但是我已经做到了,它不是我想要的,因为它需要更多的代码。
谢谢。
要在ABS居中的标题(如果你想拥有这个默认ActionBar
,只是删除方法名中的“支持”),你可能只是这样做:
在你的活动中,您onCreate
()方法:
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.abs_layout);
abs_layout
:
<?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>
现在,你应该有一个如意Actionbar
标题。如果要设置自定义背景,请将其设置为上面的布局(但不要忘记设置android:layout_height="match_parent"
)。
或:
getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.yourimage));
非常感谢。这也适用。 – David
upvote这为他人帮助谢谢。 –
据我知道有没有简单的方法。默认情况下,标题遵循材料设计指南,并且应该与页面的文本对齐,以便它位于左侧。但总有一位设计师认为Android是iOS,并希望将文本居中。所以如果这真的是你想做的事情,你将不得不走出困境。 – Eselfar
我刚刚尝试过,但不幸的是,它集中了别的东西。谢谢你的方式。 – David