您的位置:首页 > 其它

ActionBar的简单使用(修改背景颜色,文字居中,图标)

2015-12-27 11:19 549 查看
actionBar的简单使用

效果图一: 效果图二:





使用ActionBar,首先找到manifest,找到其主题:

android:theme="@style/AppTheme"


再通过style找到此主题

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<!--标题栏(actionBar)颜色-->
<item name="colorPrimary">@color/colorPrimary</item>
<!--状态栏颜色-->
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>

可以直接在此处修改颜色

最好将此主题继承Theme.Holo或其子类

值得注意的是:如果是使用Android Studio开发,请务必将新建项目后activity继承的AppCompatActivity修改为Activity,否则在修改minifest中Theme的parent后,会闪退(因为原来theme为:

parent="Theme.AppCompat.Light.DarkActionBar"

和AppCompatActivity是对应关系,所以当修改了manifest时请务必修改java代码)。

我新建了一个style,取名为myAppTheme,并在manifest中引用

<style name="myAppTheme" parent="@android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">@style/myActionBar</item> <!--设置ActionBarStyle-->
</style>


可以通过Android:actionBarStyle改变ActionBar的背景颜色,文字样式等(为了便于查看,此处颜色、尺寸我就不引用了)

<style name="myActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">#54BA42</item>
<item name="android:backgroundStacked">#54BA42</item>
<item name="android:titleTextStyle">@style/actionBarTitle</item> <!--设置titleTextStyle-->
</style>


文字具体样式在Android:titleTextStyle中设置

<style name="actionBarTitle">
<item name="android:textColor">#FFFFFF</item>
<item name="android:textSize">20sp</item>
</style>


如果要添加logo,则在manifest中添加

android:logo="@drawable/mylogo"
很明显,ActionBar上的文字在

android:label="@string/app_name"
manifest 修改后为:

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:logo="@drawable/mylogo"
android:supportsRtl="true"
android:theme="@style/myAppTheme">


当然如果你只想让此设置只作用于activity上,则将其写到activity下即可

至此,可出现效果图一的效果。

但是我们都希望标题的文字能够居中显示,即呈现效果图二的效果。

然而我并没有找到在style中直接设置文字居中的属性,故我们需要在java代码中修改

第一步:同样注意appTheme应该继承Theme.Holo或其子类,否则修改代码时会出错

第二步:新建一个xml文件,用于存放actionbar的样式

actionbar_title.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:background="@drawable/mylogo"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="ActionBar的测试"
android:textColor="#FFF"
android:textSize="20sp" />
</RelativeLayout>


第三步:在java代码中引用:

在onCreate中写:

//actionBar的设置(使用自定义的设置)
ActionBar actionBar=getActionBar();
actionBar.setCustomView(R.layout.actionbar_title);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
由于是使用自定义的布局,所以之前在style中的配置可以不需要了(关于ActionBar的背景颜色,要么按之前的在style中设置,要么在自定义的xml中将布局背景修改即可,因为我之前已经设置过style,故xml中没有重复修改)

至此,即可呈现效果图二的效果。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: