本文将通过一个实例讲解怎么实现在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. <TextView
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:text="@string/hello_world" />
  14. </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);位置是固定的.

希望读者朋友们通过这个例子能够对怎么实现在4.0及以上系统版本中自定义TitleBar有深刻理解和掌握。

Android开发如何在4.0及以上系统中自定义TitleBar的更多相关文章

  1. Android 开发之环境搭建-0

    Android 开发环境安装与配置 一.开发工具介绍 要进行Android应用程序开发,最起码要有两个工具,一个是Android SDK,它不仅为开发人员提供了丰富的编程接口,而且提供了相关的调试工具 ...

  2. [转]Android开发要看的网站(不断更新中)

    Android网址或Blog Android官网 身为Android开发者不知道这个网站就太说不过去了,上面有你任何你需要的东西 Android Developers Blog Android官网博客 ...

  3. Android系统中自定义按键的短按、双击、长按事件

    在项目中碰到这样的问题: 由于系统中的按键在底层做了重新定义或者新增了按键,此时需要在APP层对按键事件(keyevent)做分解处理,模拟Android系统做法,把keyevent分解成: 1.单击 ...

  4. 搭建Android开发环境之——Android4.0.3, 4.1, 4.2, 4.3, 4.x,及升级 ADT(22.0.5)和SDK(22.x)

    1.首先要下载相关的软件 1). JDK 6 以上 2). eclipse( Version 3.6.2  or higher ) 点击下载 3). SDK(android-sdk_r18-windo ...

  5. android开发系列之6*0.9不等于5.4

    昨天晚上我们客户端平台上面曝出了一个很奇诡的bug,那就是本来在客户端里面有个商品买6元,但是因为碰巧赶上打9折,这个时候我们很自然的处理就是6*0.9.好吧你以为so easy的事情,其实就出错了, ...

  6. Android开发(51) 摄像头自动对焦。在OpenCV图像识别中连续拍照时自动对焦和拍照。

    概述 对焦,这里所说的“焦”是指“焦距”.在拍照时,一定是需要调焦的.一般会在目标位置最清晰的时候会停止对焦.最近在处理OpenCV进行图像识别时,需要连续的调焦(对焦),并在对焦完成后进行拍照,获取 ...

  7. Android开发问题集锦-Button初始为disable状态时自定义的selector不生效问题

    1.下面是不生效的布局: selector_btn_red.xml: <?xml version="1.0" encoding="utf-8"?> ...

  8. android 开发 ScrollView 控件的一些api描述与自定义ScrollView接口回调方法

    1.正常使用ScrollView控件的一些api详解. package com.example.lenovo.mydemoapp.scrollViewDemo; import android.supp ...

  9. Android开发教程 - 使用Data Binding(八)使用自定义Interface

    本系列目录 使用Data Binding(一)介绍 使用Data Binding(二)集成与配置 使用Data Binding(三)在Activity中的使用 使用Data Binding(四)在Fr ...

随机推荐

  1. Spine的纹理导出问题

    发现美术给过来的资源,集合到unity后,发现用Spine的默认材质Spine/Skeleton有毛边问题.对比demo的图片后发现demo的图片(都是png格式)没有白色块,而自己的图片有. 原因是 ...

  2. MySql存储引擎特性对比

    下表显示了各种存储引擎的特性: 其中最常见的两种存储引擎是MyISAM和InnoDB 刚接触MySQL的时候可能会有些惊讶,竟然有不支持事务的存储引擎,学过关系型数据库理论的人都知道,事务是关系型数据 ...

  3. ftp下载显示进度

    经常用到ftp get命令下载东西,但是遇到大的文件不知道是挂了还是在运行,要是能显示就好了,于是就有了下文... 注: 红色 字体是我敲击的命令 “#” 是注释语 [root@localhost s ...

  4. javascript library

    <!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...

  5. Html 修改placeholder的颜色属性css样式

    项目需求需要修改文本框的placeholder 的文本颜色, 百度下, 备忘,我使用的是这种方法, ::-webkit-input-placeholder { /* WebKit browsers * ...

  6. 1141. RSA Attack(RSA)

    1141 越来越喜欢数论了 很有意思 先看个RSA的介绍 RSA算法是一种非对称密码算法,所谓非对称,就是指该算法需要一对密钥,使用其中一个加密,则需要用另一个才能解密. RSA的算法涉及三个参数,n ...

  7. jQuery_添加与删除元素

    一.jQuery添加元素(通过 jQuery,可以很容易地添加新元素/内容.) 1.添加新的 HTML 内容,用于添加新内容的四个 jQuery 方法(都能解析HTML标签): append() - ...

  8. Qt之HTTPS登录

    简述 HTTPS(全称:Hyper Text Transfer Protocol over Secure Socket Layer),是以安全为目标的HTTP通道,简单讲是HTTP的安全版.即HTTP ...

  9. 51nod1022 石子归并 V2

    证明w满足四边形不等式,这里w是m的附属量,形如m[i,j]=opt{m[i,k]+m[k,j]+w[i,j]},此时大多要先证明w满足条件才能进一步证明m满足条件证明m满足四边形不等式证明s[i,j ...

  10. mac 安装使用 webp 来压缩图片

    学习性网站: https://developers.google.com/speed/webp/docs/cwebp http://www.w3ctech.com//topic/1672 https: ...