首先汗一个,题目打出来我就觉得像是在写论文…… 家里生了个娃,好久没有写东西了……

 

做Android开发有一个很头疼的地方就是随着sdk的演进,很多新东西被加进来。但由于这样那样的限制, 不是所有的新玩意儿都会被加入support包里面。如果你的team里面恰好有一个矫情的PM或者designer的话,很可能发生的一件事情就是要你把一些fashion的新东西加进一个需要支持到api 11之前的项目中去。看在api 11之前的设备的活跃率大概还有20%左右的份上,作为一个敬业的dev就需要绞尽脑汁来实现这一合理的需求。

API 11开始加入的新玩意儿除了fragment就是action bar了, 这东西确实挺有用的,尤其是在一些没有按键设计的手机上, 比如MX3, Mate。如果不想激活屏幕下端的按键栏,有action bar的home button来做back的动作确实方便。

废话说了一堆,进入正题。

虽然google在support v7中提供了action bar的支持, 但那个解决方案真是太重了。 接下来说到得解法一句话就能说明白:拿掉title bar换上自己化的一个假的action bar。

 

首先我们要准备一个action bar的layout, 里面包含最基本的元素:home button, division image, title

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:id="@+id/actionbar_widget"
 4     android:layout_width="fill_parent"
 5     android:layout_height="wrap_content"
 6     android:layout_alignParentLeft="true"
 7     android:layout_alignParentTop="true"
 8     android:background="@color/actionbar_bg_color"
 9     android:orientation="horizontal" >
10     <Button
11         android:id="@+id/actionbar_btn"
12         android:layout_width="wrap_content"
13         android:layout_height="wrap_content"
14         android:layout_gravity="center_vertical"
15         android:background="@drawable/actionbar_home_btn"
16         android:padding="0dip"
17         android:paddingRight="4dip"
18         android:visibility="visible" />
19     <ImageView
20         android:layout_width="wrap_content"
21         android:layout_height="wrap_content"
22         android:src="@drawable/actionbar_division"
23         android:contentDescription="@null" />
24     <TextView
25         android:id="@+id/actionbar_title"
26         android:layout_marginLeft="4dip"
27         android:layout_width="wrap_content"
28         android:layout_height="wrap_content"
29         android:gravity="center_vertical"
30         android:layout_gravity="center_vertical"
31         android:text="@null"
32         android:textColor="@color/actionbar_text_color"
33         android:textSize="22sp"
34         android:singleLine="true" />
35 </LinearLayout>

使用的时候将这个layout include到你的activity layout的最前面:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@id/activity_frame"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"android:orientation="vertical" >
    <include
        android:id="@id/actionbar"
        android:layout_width="fill_parent"
        layout="@layout/actionbar" />
    <ListView 
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</LinearLayout>

然后在style.xml或者theme.xml里面加一个自定义的theme:

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="ActionBarTheme" parent="android:Theme.Light.NoTitleBar">
    </style>
</resources>

无论继承那个theme,只要是NoTitleBar的即可。

在AndroidManifest.xml里面讲这个theme设置给activity

        <activity 
            android:name=".Activity" 
            android:theme="@style/ActionBarTheme" >
            <intent-filter>
          <action android:name="android.intent.action.MAIN"/>
                 <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>

然后就可以在activity layout文件的Graphic layout页面看到效果了。

按键响应和title设置略去不说了。

 

另外, 如果要想为preference activity添加action bar,只需按照上面的layout写。但在onCreate()里面一定要先设置preference界面的layout再设置activity的layout, 然后再设置action bar。

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        addPreferencesFromResource(R.layout.settings);
        setContentView(R.layout.activity);
        setupActionBar();
    }

这样preference的layout就会被嵌入到id为@android:id/list的list view里面。因为preference activity是继承自List activity, 而List activity的layout是这样写的:

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

     <ListView android:id="@android:id/list"
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:background="#00FF00"
               android:layout_weight="1"
               android:drawSelectorOnTop="false"/>

     <TextView android:id="@android:id/empty"
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:background="#FF0000"
               android:text="No data"/>
 </LinearLayout>

它们插入list item时都用 com.android.internal.R.id.list 这个ID来找list view。所以这是只是一个精彩的“移形换位”, 完全安全可靠。

 

《END》

 

 

 

posted on 2014-05-21 12:47  我是猴面包的树  阅读(367)  评论(0编辑  收藏  举报