android自定义titlebar

首先,修改标题栏的宽度和背景,在style.xml中添加:

<style name="MyActivityTheme" parent="android:Theme.Light" >
       <item name="android:windowTitleBackgroundStyle">@style/windowTitleBackgroundStyle</item>
       <item name="android:windowTitleStyle">@style/windowTitleStyle</item>
       <!-- Window Header Height -->
       <item name="android:windowTitleSize">54dp</item>
   </style>
   
   <!-- Preference Settings Window Title -->
   <style name="windowTitleBackgroundStyle">
       <item name="android:background">#CCE8CF</item>
       </style>
       
       <style name="windowTitleStyle">
       <item name="android:textColor">#FF0000</item>
       <item name="android:paddingLeft">25dp</item>
       <item name="android:textSize">20sp</item>
       </style>


       然后修改AndroidMainfest.xml文件:

[html]  view plain copy
  1. <activity android:name=".MainActivity"   
  2.                 android:theme="@style/test">   
  3.           <intent-filter>   
  4.               <action android:name="android.intent.action.MAIN" />   
  5.               <category android:name="android.intent.category.LAUNCHER" />   
  6.           </intent-filter>   
  7.       </activity>  

      接着修改MainActivity:

[html]  view plain copy
  1. public void onCreate(Bundle savedInstanceState) {   
  2.        super.onCreate(savedInstanceState);   
  3.        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); //声明使用自定义标题   
  4.        setContentView(R.layout.main);   
  5.        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);//自定义布局赋值   
  6.    }  
最后在layout文件夹中添加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:orientation="horizontal" android:layout_width="fill_parent"   
  4. android:layout_height="fill_parent">   
  5.   
  6. <ImageView android:layout_width="wrap_content"   
  7. android:layout_centerVertical=’true’   
  8. android:layout_height="wrap_content"   
  9. android:src="@drawable/ic_launcher_email" />   
  10. <TextView android:layout_width="wrap_content"   
  11. android:layout_centerInParent="true"   
  12. android:layout_height="wrap_content"   
  13. android:text="自定义标题栏" />   
  14.   
  15. </RelativeLayout>  
OK,一个简单的自定义标题栏完成了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值