该实例的功能比较简单,但是通过该实例的扩展可以在自定义标题中做出菜单导航等实用的功能,为了实现自定义窗口标题,需要做以下几个步骤:

1、给自定义标题提供一个界面

2、将自定义标题应用给Activity窗口

3、把android系统为Activity设置的默认主题改为自己的主题

============================下面查看实现该例子的具体代码================================

step1、新建一个项目MyCustomTitle

step2、编写自定义标题的布局文件 /res/layout/custom_title.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="horizontal" android:layout_width="fill_parent"
  4. android:layout_height="fill_parent" android:background="@drawable/rectangle"> <!-- 指定背景,该背景自己画的 -->
  5.  
  6. <Button android:id="@+id/infoAtMeTextView"
  7. android:textColor="#FF0000" android:layout_width="wrap_content"
  8. android:layout_height="match_parent" android:layout_weight="1"
  9. android:gravity="center" android:text="\@我" />
  10. <Button android:id="@+id/infoCommentTextView"
  11. android:textColor="#FF0000" android:layout_width="wrap_content"
  12. android:layout_height="match_parent" android:layout_weight="1"
  13. android:gravity="center" android:text="评论" />
  14. <Button android:id="@+id/infoPrivateMsgTextView"
  15. android:textColor="#FF0000" android:layout_width="wrap_content"
  16. android:layout_height="match_parent" android:layout_weight="1"
  17. android:gravity="center" android:text="私信" />
  18. </LinearLayout>

step3、上面布局文件中使用的背景是一个drawable文件,该drawable文件绘制了一个长方形。该文件是/drawable/rectangle.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!-- 下面定义了一个长方形 -->
  3. <shape xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:shape="rectangle">
  5. <gradient android:angle="270" android:endColor="#1DC9CD"
  6. android:startColor="#A2E0FB" />
  7. <padding android:left="2dp" android:top="2dp" android:right="2dp"
  8. android:bottom="2dp" />
  9. </shape>

step4、将自定义标题设置到Activity中,CustomTitleActivity.java

  1. package cn.oyp.title;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.view.Window;
  6.  
  7. public class CustomTitleActivity extends Activity {
  8. /** Called when the activity is first created. */
  9. @Override
  10. public void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. //指定使用自定义标题
  13. requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
  14. setContentView(R.layout.main);
  15. //设置窗口的自定义标题布局文件
  16. getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);
  17.  
  18. }
  19. }

===================================== 读源码开始 ==============================================

然后运行该应用,发现用户设置后的自定义layout没有办法填充整个标题栏。

通过查看Android源代码得知Android系统为Activity的title默认设置了一个布局文件,该布局文件是core/res/res/layout/screen_title.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!-- Copyright (C) 2006 The Android Open Source Project
  3.  
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7.  
  8. http://www.apache.org/licenses/LICENSE-2.0
  9.  
  10. Unless required by applicable law or agreed to in writing, software
  11. distributed under the License is distributed on an "AS IS" BASIS,
  12. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. See the License for the specific language governing permissions and
  14. limitations under the License.
  15. -->
  16.  
  17. <!--
  18. This is an optimized layout for a screen, with the minimum set of features
  19. enabled.
  20. -->
  21.  
  22. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  23. android:orientation="vertical"
  24. android:fitsSystemWindows="true">
  25. <FrameLayout
  26. android:layout_width="match_parent"
  27. android:layout_height="?android:attr/windowTitleSize"
  28. style="?android:attr/windowTitleBackgroundStyle">
  29. <TextView android:id="@android:id/title"
  30. style="?android:attr/windowTitleStyle"
  31. android:background="@null"
  32. android:fadingEdge="horizontal"
  33. android:gravity="center_vertical"
  34. android:layout_width="match_parent"
  35. android:layout_height="match_parent" />
  36. </FrameLayout>
  37. <FrameLayout android:id="@android:id/content"
  38. android:layout_width="match_parent"
  39. android:layout_height="0dip"
  40. android:layout_weight="1"
  41. android:foregroundGravity="fill_horizontal|top"
  42. android:foreground="?android:attr/windowContentOverlay" />
  43. </LinearLayout>

读上一段代码可以发现该布局文件由两个帧布局构成,而其中的几个属性

?android:attr/windowTitleSize 标题高度

?android:attr/windowTitleBackgroundStyle     标题背景样式

?android:attr/windowContentOverlay 标题前景色

而这几个属性的值都是在core/res/res/values/themes.xml文件中被赋值了

  1. <style name="Theme">
  2. <item name="android:windowContentOverlay">@android:drawable/title_bar_shadow</item>
  3. <item name="android:windowTitleSize">25dp</item>
  4. <item name="android:windowTitleBackgroundStyle">@android:style/WindowTitleBackground</item>
  5. </style>

而上面的@android:style/WindowTitleBackground样式在core/res/res/values/styles.xml文件中被定义

  1. <style name="WindowTitleBackground">
  2. <item name="android:background">@android:drawable/title_bar</item>
  3. </style>

===================================== 读源码结束 ==============================================

step5、自定义样式 /res/values/style.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <!-- 该样式继承系统的默认样式 -->
  4. <style name="customTheme" parent="android:Theme">
  5. <!-- 设置标题前景色为透明 -->
  6. <item name="android:windowContentOverlay">@drawable/nocolor</item>
  7. <!-- 设置标题高度为44dp -->
  8. <item name="android:windowTitleSize">44dp</item>
  9. <!-- 设置标题背景色 -->
  10. <item name="android:windowTitleBackgroundStyle">@style/customBg</item>
  11. </style>
  12.  
  13. <!-- 定义一个背景样式 -->
  14. <style name="customBg">
  15. <item name="android:background">@drawable/rectangle</item>
  16. </style>
  17. </resources>

上面的@drawable/nocolor定义在/res/values/strings.xml文件中

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <string name="hello">该应用的目的是自定义窗口标题!</string>
  4. <string name="app_name">自定义窗口标题</string>
  5. <!-- 定义一个透明色 -->
  6. <drawable name="nocolor">#00000000</drawable>
  7. </resources>

step6、将在自定义的标题样式应用到窗口中,在描述文件AndroidManifest.xml中

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="cn.oyp.title"
  4. android:versionCode="1"
  5. android:versionName="1.0">
  6. <uses-sdk android:minSdkVersion="8" />
  7.  
  8. <application android:icon="@drawable/icon" android:label="@string/app_name">
  9. <activity android:name=".CustomTitleActivity"
  10. android:theme="@style/customTheme"><!-- 使用自定义主题 -->
  11. <intent-filter>
  12. <action android:name="android.intent.action.MAIN" />
  13. <category android:name="android.intent.category.LAUNCHER" />
  14. </intent-filter>
  15. </activity>
  16.  
  17. </application>
  18. </manifest>

step7:查看该自定义窗口的效果

=================================================================================================

  作者:欧阳鹏  欢迎转载,与人分享是进步的源泉!

  转载请保留原文地址:http://blog.csdn.net/ouyang_peng

==================================================================================================

我的Android进阶之旅------>Android自定义窗口标题实例的更多相关文章

  1. 我的Android进阶之旅------>Android中查看应用签名信息

    一.查看自己的证书签名信息 如上一篇文章<我的Android进阶之旅------>Android中制作和查看自定义的Debug版本Android签名证书>地址:http://blog ...

  2. 我的Android进阶之旅------> Android为TextView组件中显示的文本添加背景色

    通过上一篇文章 我的Android进阶之旅------> Android在TextView中显示图片方法 (地址:http://blog.csdn.net/ouyang_peng/article ...

  3. 我的Android进阶之旅------>Android疯狂连连看游戏的实现之开发游戏界面(二)

    连连看的游戏界面十分简单,大致可以分为两个区域: 游戏主界面区 控制按钮和数据显示区 1.开发界面布局 本程序使用一个RelativeLayout作为整体的界面布局元素,界面布局上面是一个自定义组件, ...

  4. 我的Android进阶之旅------>Android疯狂连连看游戏的实现之游戏效果预览(一)

    今天看完了李刚老师的<疯狂Android讲义>一书中的第18章<疯狂连连看>,从而学会了如何编写一个简单的Android疯狂连连看游戏. 开发这个流行的小游戏,难度适中,而且能 ...

  5. 我的Android进阶之旅------>Android利用Sensor(传感器)实现水平仪功能的小例

    这里介绍的水平仪,指的是比较传统的气泡水平仪,在一个透明圆盘内充满液体,液体中留有一个气泡,当一端翘起时,该气泡就会浮向翘起的一端.    利用方向传感器返回的第一个参数,实现了一个指南针小应用. 我 ...

  6. 我的Android进阶之旅------>Android颜色值(#AARRGGBB)透明度百分比和十六进制对应关系以及计算方法

    我的Android进阶之旅-->Android颜色值(RGB)所支持的四种常见形式 透明度百分比和十六进制对应关系表格 透明度 十六进制 100% FF 99% FC 98% FA 97% F7 ...

  7. 我的Android进阶之旅------>Android利用温度传感器实现带动画效果的电子温度计

    要想实现带动画效果的电子温度计,需要以下几个知识点: 1.温度传感器相关知识. 2.ScaleAnimation动画相关知识,来进行水印刻度的缩放效果. 3.android:layout_weight ...

  8. 我的Android进阶之旅------>Android实现用Android手机控制PC端的关机和重启的功能(三)Android客户端功能实现

    我的Android进阶之旅------>Android实现用Android手机控制PC端的关机和重启的功能(一)PC服务器端(地址:http://blog.csdn.net/ouyang_pen ...

  9. 我的Android进阶之旅------> Android在TextView中显示图片方法

    面试题:请说出Android SDK支持哪些方式显示富文本信息(不同颜色.大小.并包含图像的文本信息),并简要说明实现方法. 答案:Android SDK支持如下显示富文本信息的方式. 1.使用Tex ...

  10. 我的Android进阶之旅------>Android疯狂连连看游戏的实现之实现游戏逻辑(五)

    在上一篇<我的Android进阶之旅------>Android疯狂连连看游戏的实现之加载界面图片和实现游戏Activity(四)>中提到的两个类: GameConf:负责管理游戏的 ...

随机推荐

  1. Android之TextView的Span样式源代码剖析

    Android中的TextView是个显示文字的的UI类.在现实中的需求中,文字有各式各样的样式,TextView本身没有属性去设置实现.我们能够通过Android提供的 SpannableStrin ...

  2. IO流(一)File类

    1.File类:表示文件和目录路径的抽象的表示形式,可以实现文件的创建,删除,重命名等,是唯一与文件本 有关的操作类. 2.File类的API定义:public class File extends ...

  3. vue笔记二

    七.列表渲染 1.示例 <ul id="example-2"> <li v-for="(item, index) in items"> ...

  4. java中native方法的使用

    在非常多情况下,java须要调用其它语言的代码,比方c的代码.那么这个时候java中native方法就发挥作用了.以下就介绍native方法的使用. 一.JNI使用流程 a.编写带有native声明的 ...

  5. expect获取返回值

    对于获取多台server状态且不用交互须要用到expect,但有时候expect无法获取返回值.这里解释一下expect怎样获取返回值 expect -c "     spawn $1;   ...

  6. 每天一个JavaScript实例-展示设置和获取CSS样式设置

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  7. 关于解决 http 状态码200,php 文件有输出,但是不显示模板文件的问题

    一 问题 给公司搭建一个在线测试站点之后,在浏览器地址栏输入 "http://xxx.xxx.xxx/index.php",页面什么都没显示.调出浏览器的开发者工具查看,http ...

  8. Dephi泛型

    TArray TEnumerator(抽象) TEnumerable(抽象) 实际使用:TList TQueue TStack TPair TDictionary ,内部都包含 TValueEnume ...

  9. Android服务类Service具体解析

    Service有什么作用? 很多人不明确service是用来干嘛的.事实上Service作为Android四大组件之中的一个,能够理解为一个执行在后台的Activity.它适用于处理一些不干扰用户的长 ...

  10. 近期建了一个.net源代码共享群,群共享有大量网友分享的.net(C#)商业源代码

    本群创建于2013/6/21: 群里都是.net(C#)程序开发者,群共享有大量网友分享的.net(C#)商业源代码.比方:DTCMS旗舰版,hishop微分销,shopnum微分销.多用户微信公众平 ...