Android开发如何在4.0及以上系统中自定义TitleBar

Android开发如何在4.0及以上系统中自定义TitleBar


有时候,我们希望能改变一下这样单调的状况。比如,要在标题栏中增加一个用于美化界面的图标、增一个输入框或按钮之类的,怎样才能做到这一点呢?我们不妨来看一个自定义TitleBar的实际例子。

AD:

本文将通过一个实例讲解怎么实现在4.0及以上系统版本中实现自定义TitleBar,这只是我自己找到的一种方法;

xml布局文件

activity_main.xml

 
 
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  2.     xmlns:tools="http://schemas.android.com/tools" 
  3.     android:layout_width="match_parent" 
  4.     android:layout_height="match_parent" 
  5.     android:paddingBottom="@dimen/activity_vertical_margin" 
  6.     android:paddingLeft="@dimen/activity_horizontal_margin" 
  7.     android:paddingRight="@dimen/activity_horizontal_margin" 
  8.     android:paddingTop="@dimen/activity_vertical_margin" 
  9.     tools:context=".MainActivity" > 
  10.  
  11.     <TextView 
  12.         android:layout_width="wrap_content" 
  13.         android:layout_height="wrap_content" 
  14.         android:text="@string/hello_world" /> 
  15. </RelativeLayout> 

自定义的Titlebar的布局文件titlebar.xml

 
 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:layout_width="match_parent" 
  4.     android:layout_height="48dp" 
  5.     android:orientation="horizontal" > 
  6.     <ImageView 
  7.         android:layout_width="wrap_content" 
  8.         android:layout_height="wrap_content" 
  9.         android:layout_gravity="center" 
  10.         android:layout_weight="1.5" 
  11.         android:src="@drawable/ic_action_search" /> 
  12.     <TextView 
  13.         android:layout_width="wrap_content" 
  14.         android:layout_height="48dp" 
  15.         android:layout_weight="1.5" 
  16.         android:paddingTop="1dp" 
  17.         android:text="@string/app_name" 
  18.         android:textSize="14sp" /> 
  19.     <EditText 
  20.         android:id="@+id/searchparameter" 
  21.         android:layout_width="wrap_content" 
  22.         android:layout_height="48dp" 
  23.         android:layout_margin="1dp" 
  24.         android:layout_weight="5" 
  25.         android:text="ABCDEFGHIJ" 
  26.         android:textSize="14sp" /> 
  27.     <Button 
  28.         android:id="@+id/button" 
  29.         android:layout_width="wrap_content" 
  30.         android:layout_height="48dp" 
  31.         android:layout_margin="1dp" 
  32.         android:layout_weight="2" 
  33.         android:text="OK" 
  34.         android:textSize="14sp" /> 
  35. </LinearLayout> 

为布局文件修改style.xml

此处的style.xml在values-11或者value-14中,否侧会报错:you cannot combine custom titles with other title features

 
 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <resources xmlns:android="http://schemas.android.com/apk/res/android"
  3.     <style name="CustomizedWindowTitleBackground"
  4.         <item name="android:background">#047BF0</item> 
  5.     </style> 
  6.     <style name="titlebarstyle" parent="android:Theme"
  7.         <item name="android:windowTitleSize">48dp</item> 
  8.         <item name="android:windowTitleBackgroundStyle">@style/CustomizedWindowTitleBackground</item> 
  9.         <!-- All customizations that are NOT specific to a particular API-level can go here. --> 
  10.     </style> 
  11. </resources> 

在AndroidManifest.xml添加主题样式

android:theme="@style/titlebarstyle"

 
 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     package="com.example.titlebardemo" 
  4.     android:versionCode="1" 
  5.     android:versionName="1.0" > 
  6.     <uses-sdk 
  7.         android:minSdkVersion="8" 
  8.         android:targetSdkVersion="17" /> 
  9.     <application 
  10.         android:allowBackup="true" 
  11.         android:icon="@drawable/ic_launcher" 
  12.         android:label="@string/app_name" > 
  13.         <activity 
  14.             android:name="com.example.titlebardemo.MainActivity" 
  15.             android:label="@string/app_name" 
  16.            android:theme="@style/titlebarstyle" > 
  17.             <intent-filter> 
  18.                 <action android:name="android.intent.action.MAIN" /> 
  19.                 <category android:name="android.intent.category.LAUNCHER" /> 
  20.             </intent-filter> 
  21.         </activity> 
  22.     </application> 
  23. </manifest> 

MainActivity.java中添加实现代码

 
 
  1. protected void onCreate(Bundle savedInstanceState) { 
  2.         super.onCreate(savedInstanceState); 
  3.         requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 
  4.         setContentView(R.layout.activity_main); 
  5.         getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, 
  6.                 R.layout.titlebar); 
  7.     } 

代码中requestWindowFeature(Window.FEATURECUSTOMTITLE);和getWindow().setFeatureInt(Window.FEATURECUSTOMTITLE,
R.layout.titlebar);位置是固定的.

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值