ActionBar的简单使用(修改背景颜色,文字居中,图标)
2015-12-27 11:19
549 查看
actionBar的简单使用
效果图一: 效果图二:
使用ActionBar,首先找到manifest,找到其主题:
再通过style找到此主题
可以直接在此处修改颜色
最好将此主题继承Theme.Holo或其子类
值得注意的是:如果是使用Android Studio开发,请务必将新建项目后activity继承的AppCompatActivity修改为Activity,否则在修改minifest中Theme的parent后,会闪退(因为原来theme为:
和AppCompatActivity是对应关系,所以当修改了manifest时请务必修改java代码)。
我新建了一个style,取名为myAppTheme,并在manifest中引用
可以通过Android:actionBarStyle改变ActionBar的背景颜色,文字样式等(为了便于查看,此处颜色、尺寸我就不引用了)
文字具体样式在Android:titleTextStyle中设置
如果要添加logo,则在manifest中添加
当然如果你只想让此设置只作用于activity上,则将其写到activity下即可
至此,可出现效果图一的效果。
但是我们都希望标题的文字能够居中显示,即呈现效果图二的效果。
然而我并没有找到在style中直接设置文字居中的属性,故我们需要在java代码中修改
第一步:同样注意appTheme应该继承Theme.Holo或其子类,否则修改代码时会出错
第二步:新建一个xml文件,用于存放actionbar的样式
actionbar_title.xml
第三步:在java代码中引用:
在onCreate中写:
至此,即可呈现效果图二的效果。
效果图一: 效果图二:
使用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中没有重复修改)
至此,即可呈现效果图二的效果。
相关文章推荐
- 传统企业如何搭上"互联网+"的大船
- 传统企业如何搭上"互联网+"的大船
- js获取url传递参数
- Android调用微信登陆、分享、支付
- 老男孩运维前后的改变
- 图片加载框架Fresco使用总结
- MongoDB系列-初识MongoDB
- 模仿美团下拉刷新
- htop,dstat,top,ps命令的使用
- Java随机数总结
- [翻译] AKKA笔记- ACTORSYSTEM (配置CONFIGURATION 与调度SCHEDULING) - 4(一)
- C++对象 内存分布
- Ghost博客数据库迁移至MySQL
- Tomcat中监控JVM内存使用情况(问题补充)
- CALayer与UIView及一些基本的3D变换
- 关于Git@OSC的那点事
- android解析xml一直报错org.xmlpull.v1.XmlPullParserException
- matlab 中max函数用法
- 算法导论 主项定理
- C#检测网络连接