改写Android的header

一、编写header用的xml

  文件代码如下,保存为title.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal"  
    android:layout_width="fill_parent"
    android:background="@drawable/tittle_bg"
    android:layout_height="wrap_content">
               	
  <Button android:id="@+id/preButton"  
		android:layout_width="wrap_content" 
		android:layout_height="wrap_content"
		android:layout_gravity="center_vertical"
		android:layout_marginLeft="5px"
		android:textSize="18dip"
		android:text="返回"
		android:textColor="#ddd"
		android:paddingLeft="8px"
		android:paddingRight="2px"
		android:background="@drawable/back_button_bg">  
  </Button>
  <TextView 		
		android:id="@+id/titleView"
		android:layout_width="wrap_content" 
		android:layout_height="wrap_content"
		android:layout_weight="1"
		android:textSize="18dip"
		android:textStyle="bold"
		android:textColor="#ddd"
		android:layout_gravity="center"
		android:gravity="center"
		android:paddingLeft="5dip"
		android:paddingRight="5dip"/>
  <Button android:id="@+id/nextButtion"  
		android:layout_width="wrap_content" 
		android:layout_height="wrap_content"
		android:layout_gravity="center_vertical"
		android:layout_marginRight="5px"
		android:paddingLeft="2px"
		android:paddingRight="2px"
		android:textSize="18dip"
		android:text="首页"
		android:textColor="#ddd"
		android:background="@drawable/next_button_bg">   
  </Button>                 
</LinearLayout> 
	              

              

二、在Activity中添加方法

  在每个要改写headerActivity中(最好是写在基类里,然后每个Activity继承就可以了),onCreate方法里写如下的代码。

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.base);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);


完成前面这两步,图标、文字和背景都有了,但是两边会留有空白。接下来的两步就是为了解决这个问题。

 

三、添加样式文件

  添加如下样式,将其保存为custom_style.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
     <style name="CustomWindowTitleText" parent="android:TextAppearance.WindowTitle">
          <item name="android:textSize">22dip</item>
          <item name="android:textColor">#ddd</item>
          <item name="android:textStyle">bold</item>
          <item name="android:gravity">center_horizontal</item>
        
          <item name="android:paddingRight">8dip</item>
          <item name="android:paddingLeft">8dip</item>
          
     </style>
     <!-- Changes the background color of the title bar -->
     <style name="CustomWindowTitleBackground">
           <item name="android:background">@drawable/nav_bg</item>
     </style>

     <!-- Set the theme for the window title -->
     <!-- NOTE: setting android:textAppearence to style defined above -->
     <style name="CustomWindowTitle" parent="*android:WindowTitle">
          <item name="android:layout_gravity">center</item>
          <item name="android:gravity">center</item>
          <item name="android:textAppearance">@style/CustomWindowTitleText</item>
          <item name="android:shadowDx">0</item>
          <item name="android:shadowDy">0</item>
          <item name="android:shadowRadius">5</item>
          <item name="android:shadowColor">#1155CC</item>
      </style>
      <!-- Override properties in the default theme -->
      <!-- NOTE: you must explicitly the windowTitleSize property, the title bar will not re-size automatically, text will be clipped -->
      <style name="CustomTheme" parent="android:Theme">
           <item name="android:windowTitleSize">36dip</item>
           <item name="android:windowTitleStyle">@style/CustomWindowTitle</item>
           <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>
      </style>
      <color name="seg_list_color">#1368ac</color>
</resources>


 


四、在manifest改变系统的theme

  在manifest的Application标签,在Theme属性下选择第三步中的样式文件中所创建的CustomTheme

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值