Android开发如何在4.0及以上系统中自定义TitleBar
本文将通过一个实例讲解怎么实现在4.0及以上系统版本中实现自定义TitleBar,这只是我自己找到的一种方法;
xml布局文件
activity_main.xml
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- tools:context=".MainActivity" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/hello_world" />
- </RelativeLayout>
自定义的Titlebar的布局文件titlebar.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="48dp"
- android:orientation="horizontal" >
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:layout_weight="1.5"
- android:src="@drawable/ic_action_search" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="48dp"
- android:layout_weight="1.5"
- android:paddingTop="1dp"
- android:text="@string/app_name"
- android:textSize="14sp" />
- <EditText
- android:id="@+id/searchparameter"
- android:layout_width="wrap_content"
- android:layout_height="48dp"
- android:layout_margin="1dp"
- android:layout_weight="5"
- android:text="ABCDEFGHIJ"
- android:textSize="14sp" />
- <Button
- android:id="@+id/button"
- android:layout_width="wrap_content"
- android:layout_height="48dp"
- android:layout_margin="1dp"
- android:layout_weight="2"
- android:text="OK"
- android:textSize="14sp" />
- </LinearLayout>
为布局文件修改style.xml
此处的style.xml在values-11或者value-14中,否侧会报错:you cannot combine custom titles with other title features
- <?xml version="1.0" encoding="utf-8"?>
- <resources xmlns:android="http://schemas.android.com/apk/res/android">
- <style name="CustomizedWindowTitleBackground">
- <item name="android:background">#047BF0</item>
- </style>
- <style name="titlebarstyle" parent="android:Theme">
- <item name="android:windowTitleSize">48dp</item>
- <item name="android:windowTitleBackgroundStyle">@style/CustomizedWindowTitleBackground</item>
- <!-- All customizations that are NOT specific to a particular API-level can go here. -->
- </style>
- </resources>
在AndroidManifest.xml添加主题样式
android:theme="@style/titlebarstyle"
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.titlebardemo"
- android:versionCode="1"
- android:versionName="1.0" >
- <uses-sdk
- android:minSdkVersion="8"
- android:targetSdkVersion="17" />
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name" >
- <activity
- android:name="com.example.titlebardemo.MainActivity"
- android:label="@string/app_name"
- android:theme="@style/titlebarstyle" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- </manifest>
MainActivity.java中添加实现代码
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
- setContentView(R.layout.activity_main);
- getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
- R.layout.titlebar);
- }
代码中requestWindowFeature(Window.FEATURECUSTOMTITLE);和getWindow().setFeatureInt(Window.FEATURECUSTOMTITLE, R.layout.titlebar);位置是固定的.
希望读者朋友们通过这个例子能够对怎么实现在4.0及以上系统版本中自定义TitleBar有深刻理解和掌握。
Android开发如何在4.0及以上系统中自定义TitleBar的更多相关文章
- Android 开发之环境搭建-0
Android 开发环境安装与配置 一.开发工具介绍 要进行Android应用程序开发,最起码要有两个工具,一个是Android SDK,它不仅为开发人员提供了丰富的编程接口,而且提供了相关的调试工具 ...
- [转]Android开发要看的网站(不断更新中)
Android网址或Blog Android官网 身为Android开发者不知道这个网站就太说不过去了,上面有你任何你需要的东西 Android Developers Blog Android官网博客 ...
- Android系统中自定义按键的短按、双击、长按事件
在项目中碰到这样的问题: 由于系统中的按键在底层做了重新定义或者新增了按键,此时需要在APP层对按键事件(keyevent)做分解处理,模拟Android系统做法,把keyevent分解成: 1.单击 ...
- 搭建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 ...
- android开发系列之6*0.9不等于5.4
昨天晚上我们客户端平台上面曝出了一个很奇诡的bug,那就是本来在客户端里面有个商品买6元,但是因为碰巧赶上打9折,这个时候我们很自然的处理就是6*0.9.好吧你以为so easy的事情,其实就出错了, ...
- Android开发(51) 摄像头自动对焦。在OpenCV图像识别中连续拍照时自动对焦和拍照。
概述 对焦,这里所说的“焦”是指“焦距”.在拍照时,一定是需要调焦的.一般会在目标位置最清晰的时候会停止对焦.最近在处理OpenCV进行图像识别时,需要连续的调焦(对焦),并在对焦完成后进行拍照,获取 ...
- Android开发问题集锦-Button初始为disable状态时自定义的selector不生效问题
1.下面是不生效的布局: selector_btn_red.xml: <?xml version="1.0" encoding="utf-8"?> ...
- android 开发 ScrollView 控件的一些api描述与自定义ScrollView接口回调方法
1.正常使用ScrollView控件的一些api详解. package com.example.lenovo.mydemoapp.scrollViewDemo; import android.supp ...
- Android开发教程 - 使用Data Binding(八)使用自定义Interface
本系列目录 使用Data Binding(一)介绍 使用Data Binding(二)集成与配置 使用Data Binding(三)在Activity中的使用 使用Data Binding(四)在Fr ...
随机推荐
- Properties读取资源文件的四种方法
package com.action; import java.io.InputStream; import java.util.Locale; import java.util.Properties ...
- Swagger PHP使用指南
先说什么是Swagger, Swagger的使用目的是方便优美的呈现出接口API的各种定义, 生成API文档, 包括参数, 路径之类. 有时后端改了API的参数或者其他设置, 前端直接看这个Swagg ...
- HttpClient基本用法
<Apache HttpClient 4.3开发指南> Apache HttpClient 4系列已经发布很久了,但由于它与HttpClient 3.x版本完全不兼容,以至于业内采用此库的 ...
- Ajax动态滚动加载数据
看新浪微博,人人网都有这样的效果:滚动条滚动到最下面的时候,新的数据就被自动加载出来了,今天亲自尝试了一下这个效果的实现. 最开始在CSDN上写了一版,功能比较简单,今天又增加了一个小功能:翻页到指定 ...
- chrome开发配置(四)生成项目及配置库引用
1.运行gclient runhooks --force .会重新下载个python 2.7版本的,并且下载好几个文件,大概1个多小时:
- java读取package中的properties文件java.util.MissingResourceException
文件结构: /build/classes/d914/Hello.class /build/classes/d914/mess.properties /build/classes/d914/mess_z ...
- 配置VNCserver
翻译:yunqing原作者:Thomas Chung出处:http://fedoranews.org/tchung/vnc/ 声明: 版权属原作者Thomas Chung所有,转载请注明出处. 再说一 ...
- c# 将字符串转换为逻辑表达式(字符串转换布尔)
比如:string str="6>5"; 要的效果是:bool result=6>5 方案一: 命名空间:System.Data: DataTable dt = new ...
- IntelliJ IDEA12.1.1 使用 相关记录
最近在切换ide 从eclipse平台上转移到 IDEA 其它的都比较容易,让我烦恼的是使用idea 发布工程,使用tomcat 调试 热部署.期间碰到各种问题,纠结好久了:终于成功完成了,特意记 ...
- 在linux系统下检查postgresql数据库安装,登录数据库及简单的查看数据库
1. 检查Linux系统是否安装数据库 首先查看自己的系统是否安装了postgresql数据库命令如下: rpm -qa | grep postgresql 如果没有显示查询结果(如下图所示)说 ...