如何通过 Styles.XML 集中自定义操作栏的标题文本
How to Centralize Title text for Custom Action Bar through Styles.XML
我刚刚从我的 styles.xml class 创建了一个自定义操作栏,它以我想要的方式显示。我已经从清单文件中激活了它。
<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 文件中操作栏上的标题文本。请任何人帮助如何做到这一点。
我试过使用这个:<item name="android:layout_gravity">center</item>
。但是没有效果。
我在网上看到的大多数建议都需要单独创建一个工具栏布局。但我已经做到了,但它不是我想要的,因为它需要更多代码。
谢谢。
要在 ABS 中有一个居中的标题(如果你想在默认 ActionBar
中有这个,只需删除方法名称中的 "support"),你可以这样做:
在您的 Activity 中,在您的 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));
我刚刚从我的 styles.xml class 创建了一个自定义操作栏,它以我想要的方式显示。我已经从清单文件中激活了它。
<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 文件中操作栏上的标题文本。请任何人帮助如何做到这一点。
我试过使用这个:<item name="android:layout_gravity">center</item>
。但是没有效果。
我在网上看到的大多数建议都需要单独创建一个工具栏布局。但我已经做到了,但它不是我想要的,因为它需要更多代码。
谢谢。
要在 ABS 中有一个居中的标题(如果你想在默认 ActionBar
中有这个,只需删除方法名称中的 "support"),你可以这样做:
在您的 Activity 中,在您的 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));