Andorid 自定义标题栏

效果如图:


先在strings.xml 中定义主题

  1.    <style name="customTitlebg" >  
  2.     <item name="android:background">@drawable/title_bg</item>  
  3. </style>  
  4.      
  5. <style name="titlebar" parent="android:Theme">  
  6.     <item name="android:windowTitleSize">40dp</item>   
  7.     <item name="android:windowTitleBackgroundStyle">@style/customTitlebg</item>   
  8. </style>  

在AndroidManifest.xml,application标签中改为使用我们自定义的主题
  1. <application  
  2.         android:icon="@drawable/ic_launcher"  
  3.         android:label="@string/app_name"  
  4.         android:theme="@style/titlebar" >  
  5.         <activity  
  6.             android:name=".MainActivity"  
  7. ...  

下面是自定义标题栏的实现 title_bar.xml
  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.     <ImageView android:layout_width="wrap_content"   
  6.         android:layout_centerVertical="true"  
  7.         android:layout_height="wrap_content"   
  8.         android:src="@drawable/title_home_normal" />  
  9.       
  10.     <TextView android:layout_width="wrap_content"   
  11.         android:layout_centerInParent="true"   
  12.         android:layout_height="wrap_content"   
  13.         android:textColor="#000000"  
  14.         android:text="自定义标题栏" />  
  15.   
  16.     <ImageView  
  17.         android:layout_width="wrap_content"  
  18.         android:layout_height="wrap_content"  
  19.         android:layout_alignParentRight="true"  
  20.         android:layout_centerVertical="true"  
  21.         android:src="@drawable/title_new_normal" />  
  22.   
  23. </RelativeLayout>  
最后修改Activity
  1. public void onCreate(Bundle savedInstanceState) {  
  2.     super.onCreate(savedInstanceState);  
  3.     requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); //声明使用自定义标题   
  4.     setContentView(R.layout.tabhost);  
  5.     getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar);//自定义布局赋值   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值