android开发training之actionbar之3

来源:互联网 发布:周杰伦演唱会知乎 编辑:程序博客网 时间:2024/04/30 07:01

装饰action bar


一、使用默认的action bar

Android theme有两种theme支持action bar


  • Theme.Holo for a "dark" theme.
  • Theme.Holo.Light for a "light" theme.
可以在<application> 中定义应用的全局theme也可以在<activity> 中单独定义一个activity的theme。例如:

<application android:theme="@android:style/Theme.Holo.Light" ... />

效果如下:


action bar是黑色而其余部分是light color使用Theme.Holo.Light.DarkActionBartheme.

当使用支持库时,用下面的方法:

When using the Support Library, you must instead use the Theme.AppCompat themes:

  • Theme.AppCompat for the "dark" theme.
  • Theme.AppCompat.Light for the "light" theme.
  • Theme.AppCompat.Light.DarkActionBar for the light theme with a dark action bar.

二、自己定制的action bar

Android3.0及以上:

像下面这样改变action bar的背景色:


res/values/themes.xml

<?xml version="1.0" encoding="utf-8"?><resources>    <!-- the theme applied to the application or activity -->    <style name="CustomActionBarTheme"           parent="@android:style/Theme.Holo.Light.DarkActionBar">        <item name="android:actionBarStyle">@style/MyActionBar</item>    </style>    <!-- ActionBar styles -->    <style name="MyActionBar"           parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">        <item name="android:background">@drawable/orange</item>    </style></resources>

在整个app或某个activity中应用主题:

<application android:theme="@style/CustomActionBarTheme" ... />
android2.1以下就不指明怎么用了。

三、自定义action bar中文字。

实现如下效果:


<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- the theme applied to the application or activity -->
    <style name="CustomActionBarTheme" parent="@android:style/Theme.Holo.Light.DarkActionBar">
        <item name="android:actionBarStyle">@style/MyActionBar</item>
    </style>

    <!-- ActionBar styles -->
    <style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
        <item name="android:background">@drawable/orange</item>
        <item name="android:titleTextStyle">@style/MyActionBarTitleText</item>
    </style>


    <!-- ActionBar title text -->
    <style name="MyActionBarTitleText" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title">
        <item name="android:textColor">@color/actionbar_text</item>
    </style>
</resources>

android2.1以下就不指明怎么用了。

四、将action bar变成透明的效果,如下:


Android 3.0以上,自己定制的theme的父theme应该是Theme.Holo 例如:


<resources>    <!-- the theme applied to the application or activity -->    <style name="CustomActionBarTheme"           parent="@android:style/Theme.Holo">        <item name="android:windowActionBarOverlay">true</item>    </style></resources>
但是会有一个问题,顶部的textview在action bar下面,用下面的方法解决。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingTop="?android:attr/actionBarSize">    ...</RelativeLayout>

http://download.csdn.net/detail/xliubaox/8304183  源代码

0 0
原创粉丝点击