ActionBar简单实用

来源:互联网 发布:windows phone浏览器 编辑:程序博客网 时间:2024/05/21 06:54

actionBar的简单使用

效果图一:                                                                                                                       效果图二:

                                


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

[html] view plain copy
  1. android:theme="@style/AppTheme"  

再通过style找到此主题

[html] view plain copy
  1. <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">  
  2.         <!-- Customize your theme here. -->  
  3.         <!--标题栏(actionBar)颜色-->  
  4.         <item name="colorPrimary">@color/colorPrimary</item>  
  5.         <!--状态栏颜色-->  
  6.         <item name="colorPrimaryDark">@color/colorPrimaryDark</item>  
  7.         <item name="colorAccent">@color/colorAccent</item>  
  8.     </style>  

可以直接在此处修改颜色

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

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

parent="Theme.AppCompat.Light.DarkActionBar"
和AppCompatActivity是对应关系,所以当修改了manifest时请务必修改java代码)。

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

[html] view plain copy
  1. <style name="myAppTheme" parent="@android:style/Theme.Holo.Light">  
  2.         <item name="android:actionBarStyle">@style/myActionBar</item> <!--设置ActionBarStyle-->  
  3.     </style>  

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

[html] view plain copy
  1. <style name="myActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">  
  2.         <item name="android:background">#54BA42</item>  
  3.         <item name="android:backgroundStacked">#54BA42</item>  
  4.         <item name="android:titleTextStyle">@style/actionBarTitle</item> <!--设置titleTextStyle-->  
  5.     </style>  

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

[html] view plain copy
  1. <style name="actionBarTitle">  
  2.         <item name="android:textColor">#FFFFFF</item>  
  3.         <item name="android:textSize">20sp</item>  
  4.     </style>  

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

[html] view plain copy
  1. android:logo="@drawable/mylogo"  
很明显,ActionBar上的文字在

[html] view plain copy
  1. android:label="@string/app_name"  
manifest 修改后为:
[html] view plain copy
  1. <application  
  2.        android:allowBackup="true"  
  3.        android:icon="@mipmap/ic_launcher"  
  4.        android:label="@string/app_name"  
  5.        android:logo="@drawable/mylogo"  
  6.        android:supportsRtl="true"  
  7.        android:theme="@style/myAppTheme">  

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

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


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

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

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

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

actionbar_title.xml

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical">  
  6.   
  7.     <ImageView  
  8.         android:layout_width="24dp"  
  9.         android:layout_height="24dp"  
  10.         android:background="@drawable/mylogo"  
  11.         android:layout_centerVertical="true"  
  12.         android:layout_marginLeft="5dp"/>  
  13.     <TextView  
  14.         android:layout_width="wrap_content"  
  15.         android:layout_height="wrap_content"  
  16.         android:layout_centerInParent="true"  
  17.         android:text="ActionBar的测试"  
  18.         android:textColor="#FFF"  
  19.         android:textSize="20sp" />  
  20. </RelativeLayout>  

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

在onCreate中写:

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

至此,即可呈现效果图二的效果。

0 0
原创粉丝点击