【Android UI】自定义带按钮的标题栏

自定义标题栏在很多的android app中很常见,可以说是一种很有用的UI设计方法。自

己也本着学习的态度,经过一番各种坑,终于实现了,现总结如下:

一:大致流程

1.      对指定的android activity设置自定义主题风格,其中自定义主题风格是关键

在android 4.0以上版本中如果使用Theme.Holo或者Theme.Light等,程序会

一直报错误-you cannot combine custom title with other feature titles

2.      在对应的Activity中加入代码

[java] view plaincopy

  1. super.onCreate(savedInstanceState);  

  2. requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);  

  3. setContentView(R.layout.main);  

  4. getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.mycustomtitle);  

3.      在styles.xml使用如下的自定义主题,发现只有使用这个默认主题才不出第一步的

错误,真是各种坑啊!

[html] view plaincopy

  1. <resources>  

  2.   

  3.     <style name="WindowTitleBackground" >     

  4.         <item name="android:background">@color/blue</item>         

  5.     </style>  

  6.     <style name="MyTheme" parent="android:Theme">  

  7.         <item name="android:windowTitleSize">60dp</item>  

  8.         <item name="android:windowTitleBackgroundStyle">@style/WindowTitleBackground</item>                  

  9.     </style>  

  10.   

  11. </resources>  

4.      关键技巧,使用RelativeLayout来对齐自定义Title的组件

二:测试MainActivity源代码

[java] view plaincopy

  1. package com.gloomyfish.titledemo;  

  2.   

  3. import android.app.Activity;  

  4. import android.os.Bundle;  

  5. import android.view.Window;  

  6.   

  7. public class MainActivity extends Activity {  

  8.   

  9.     @Override  

  10.     protected void onCreate(Bundle savedInstanceState) {  

  11.         super.onCreate(savedInstanceState);  

  12.         requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);  

  13.         setContentView(R.layout.main);  

  14.         getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.mycustomtitle);  

  15.     }  

  16.       

  17. }  

三:XML资源文件

mycustomtitle.xml的内容

[java] view plaincopy

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  

  2.     android:layout_width="fill_parent"  

  3.     android:layout_gravity="fill_horizontal"  

  4.     android:orientation="horizontal"  

  5.     android:layout_height="fill_parent" >  

  6.     <Button  android:id="@+id/header_left_btn"  

  7.         android:layout_width="wrap_content"  

  8.         android:layout_height="wrap_content"  

  9.         android:layout_alignParentLeft="true"  

  10.         android:layout_marginLeft="5dp"  

  11.         android:layout_centerVertical="true"  

  12.         android:text="返回"  

  13.         android:textColor="#000000"/>  

  14.       

  15.         <TextView android:id="@+id/header_text"  

  16.             android:layout_width="fill_parent"  

  17.             android:layout_height="fill_parent"  

  18.             android:layout_toRightOf="@+id/header_left_btn"  

  19.             android:layout_toLeftOf="@+id/header_right_btn"  

  20.             android:text="My Title Bar"  

  21.             android:textSize="20sp"  

  22.             android:textStyle="bold"  

  23.             android:textColor="#FFFFFF"  

  24.             android:gravity="center"  

  25.             android:layout_centerHorizontal="true"  

  26.             android:layout_centerVertical="true"  

  27.             android:singleLine="true" />  

  28.              

  29.         <Button  android:id="@+id/header_right_btn"  

  30.             android:layout_width="wrap_content"  

  31.              android:layout_height="wrap_content"  

  32.             android:layout_alignParentRight="true"  

  33.             android:layout_marginRight="5dp"  

  34.              android:layout_centerVertical="true"  

  35.              android:text="图片"    

  36.              android:textColor="#000000"/>  

  37.   

  38. </RelativeLayout>  

最后别忘记在androi的manifest配置文件中加上自定义的主题

[html] view plaincopy

  1. android:theme="@style/MyTheme"   

同时还要删除IDE默认生成的那些appTheme,不然也会一直报错!

最终效果如下:

【Android UI】自定义带按钮的标题栏

实现这个自定义标题栏的时候,看到stackoverflow上面说

如果使用Theme.Holo一定要换成Theme.Holo.NoActionBar主题

可以我换了以后发现,还是各种坑,一直不出效果。

所以我推荐一定要使用android:theme


你可能感兴趣的:(【Android UI】自定义带按钮的标题栏)