我的Android进阶之旅------>Android自定义窗口标题实例
该实例的功能比较简单,但是通过该实例的扩展可以在自定义标题中做出菜单导航等实用的功能,为了实现自定义窗口标题,需要做以下几个步骤:
1、给自定义标题提供一个界面
2、将自定义标题应用给Activity窗口
3、把android系统为Activity设置的默认主题改为自己的主题
============================下面查看实现该例子的具体代码================================
step1、新建一个项目MyCustomTitle
step2、编写自定义标题的布局文件 /res/layout/custom_title.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="@drawable/rectangle"> <!-- 指定背景,该背景自己画的 --> <Button android:id="@+id/infoAtMeTextView"
android:textColor="#FF0000" android:layout_width="wrap_content"
android:layout_height="match_parent" android:layout_weight="1"
android:gravity="center" android:text="\@我" />
<Button android:id="@+id/infoCommentTextView"
android:textColor="#FF0000" android:layout_width="wrap_content"
android:layout_height="match_parent" android:layout_weight="1"
android:gravity="center" android:text="评论" />
<Button android:id="@+id/infoPrivateMsgTextView"
android:textColor="#FF0000" android:layout_width="wrap_content"
android:layout_height="match_parent" android:layout_weight="1"
android:gravity="center" android:text="私信" />
</LinearLayout>
step3、上面布局文件中使用的背景是一个drawable文件,该drawable文件绘制了一个长方形。该文件是/drawable/rectangle.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- 下面定义了一个长方形 -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:angle="270" android:endColor="#1DC9CD"
android:startColor="#A2E0FB" />
<padding android:left="2dp" android:top="2dp" android:right="2dp"
android:bottom="2dp" />
</shape>
step4、将自定义标题设置到Activity中,CustomTitleActivity.java
package cn.oyp.title; import android.app.Activity;
import android.os.Bundle;
import android.view.Window; public class CustomTitleActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//指定使用自定义标题
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
//设置窗口的自定义标题布局文件
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title); }
}
===================================== 读源码开始 ==============================================
然后运行该应用,发现用户设置后的自定义layout没有办法填充整个标题栏。
通过查看Android源代码得知Android系统为Activity的title默认设置了一个布局文件,该布局文件是core/res/res/layout/screen_title.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2006 The Android Open Source Project Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
--> <!--
This is an optimized layout for a screen, with the minimum set of features
enabled.
--> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:fitsSystemWindows="true">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="?android:attr/windowTitleSize"
style="?android:attr/windowTitleBackgroundStyle">
<TextView android:id="@android:id/title"
style="?android:attr/windowTitleStyle"
android:background="@null"
android:fadingEdge="horizontal"
android:gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
<FrameLayout android:id="@android:id/content"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:foregroundGravity="fill_horizontal|top"
android:foreground="?android:attr/windowContentOverlay" />
</LinearLayout>
读上一段代码可以发现该布局文件由两个帧布局构成,而其中的几个属性
?android:attr/windowTitleSize 标题高度
?android:attr/windowTitleBackgroundStyle 标题背景样式
?android:attr/windowContentOverlay 标题前景色
而这几个属性的值都是在core/res/res/values/themes.xml文件中被赋值了
<style name="Theme">
<item name="android:windowContentOverlay">@android:drawable/title_bar_shadow</item>
<item name="android:windowTitleSize">25dp</item>
<item name="android:windowTitleBackgroundStyle">@android:style/WindowTitleBackground</item>
</style>
而上面的@android:style/WindowTitleBackground样式在core/res/res/values/styles.xml文件中被定义
<style name="WindowTitleBackground">
<item name="android:background">@android:drawable/title_bar</item>
</style>
===================================== 读源码结束 ==============================================
step5、自定义样式 /res/values/style.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- 该样式继承系统的默认样式 -->
<style name="customTheme" parent="android:Theme">
<!-- 设置标题前景色为透明 -->
<item name="android:windowContentOverlay">@drawable/nocolor</item>
<!-- 设置标题高度为44dp -->
<item name="android:windowTitleSize">44dp</item>
<!-- 设置标题背景色 -->
<item name="android:windowTitleBackgroundStyle">@style/customBg</item>
</style> <!-- 定义一个背景样式 -->
<style name="customBg">
<item name="android:background">@drawable/rectangle</item>
</style>
</resources>
上面的@drawable/nocolor定义在/res/values/strings.xml文件中
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">该应用的目的是自定义窗口标题!</string>
<string name="app_name">自定义窗口标题</string>
<!-- 定义一个透明色 -->
<drawable name="nocolor">#00000000</drawable>
</resources>
step6、将在自定义的标题样式应用到窗口中,在描述文件AndroidManifest.xml中
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.oyp.title"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".CustomTitleActivity"
android:theme="@style/customTheme"><!-- 使用自定义主题 -->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity> </application>
</manifest>
step7:查看该自定义窗口的效果
=================================================================================================
作者:欧阳鹏 欢迎转载,与人分享是进步的源泉!
转载请保留原文地址:http://blog.csdn.net/ouyang_peng
==================================================================================================
我的Android进阶之旅------>Android自定义窗口标题实例的更多相关文章
- 我的Android进阶之旅------>Android中查看应用签名信息
一.查看自己的证书签名信息 如上一篇文章<我的Android进阶之旅------>Android中制作和查看自定义的Debug版本Android签名证书>地址:http://blog ...
- 我的Android进阶之旅------> Android为TextView组件中显示的文本添加背景色
通过上一篇文章 我的Android进阶之旅------> Android在TextView中显示图片方法 (地址:http://blog.csdn.net/ouyang_peng/article ...
- 我的Android进阶之旅------>Android疯狂连连看游戏的实现之开发游戏界面(二)
连连看的游戏界面十分简单,大致可以分为两个区域: 游戏主界面区 控制按钮和数据显示区 1.开发界面布局 本程序使用一个RelativeLayout作为整体的界面布局元素,界面布局上面是一个自定义组件, ...
- 我的Android进阶之旅------>Android疯狂连连看游戏的实现之游戏效果预览(一)
今天看完了李刚老师的<疯狂Android讲义>一书中的第18章<疯狂连连看>,从而学会了如何编写一个简单的Android疯狂连连看游戏. 开发这个流行的小游戏,难度适中,而且能 ...
- 我的Android进阶之旅------>Android利用Sensor(传感器)实现水平仪功能的小例
这里介绍的水平仪,指的是比较传统的气泡水平仪,在一个透明圆盘内充满液体,液体中留有一个气泡,当一端翘起时,该气泡就会浮向翘起的一端. 利用方向传感器返回的第一个参数,实现了一个指南针小应用. 我 ...
- 我的Android进阶之旅------>Android颜色值(#AARRGGBB)透明度百分比和十六进制对应关系以及计算方法
我的Android进阶之旅-->Android颜色值(RGB)所支持的四种常见形式 透明度百分比和十六进制对应关系表格 透明度 十六进制 100% FF 99% FC 98% FA 97% F7 ...
- 我的Android进阶之旅------>Android利用温度传感器实现带动画效果的电子温度计
要想实现带动画效果的电子温度计,需要以下几个知识点: 1.温度传感器相关知识. 2.ScaleAnimation动画相关知识,来进行水印刻度的缩放效果. 3.android:layout_weight ...
- 我的Android进阶之旅------>Android实现用Android手机控制PC端的关机和重启的功能(三)Android客户端功能实现
我的Android进阶之旅------>Android实现用Android手机控制PC端的关机和重启的功能(一)PC服务器端(地址:http://blog.csdn.net/ouyang_pen ...
- 我的Android进阶之旅------> Android在TextView中显示图片方法
面试题:请说出Android SDK支持哪些方式显示富文本信息(不同颜色.大小.并包含图像的文本信息),并简要说明实现方法. 答案:Android SDK支持如下显示富文本信息的方式. 1.使用Tex ...
- 我的Android进阶之旅------>Android疯狂连连看游戏的实现之实现游戏逻辑(五)
在上一篇<我的Android进阶之旅------>Android疯狂连连看游戏的实现之加载界面图片和实现游戏Activity(四)>中提到的两个类: GameConf:负责管理游戏的 ...
随机推荐
- Android——Activity的生命周期
一,Demo測试Activity的生命周期 写两个Activity: package com.example.activity_04; import android.os.Bundle; import ...
- GridView后台绑定数据列表方法
在很多时候数据绑定都是知道了数据表中的表字段来绑定GridView控件的,那时候我就有个想法希望通过表明来查询数据库中的字段来动态的绑定GirdView控件数据并提供了相关的操作列,在网上找了一些资料 ...
- NYOJ82 迷宫寻宝(一)【BFS】
迷宫寻宝(一) 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描写叙述 一个叫ACM的寻宝者找到了一个藏宝图.它依据藏宝图找到了一个迷宫,这是一个非常特别的迷宫,迷宫里有N个 ...
- 倍福TwinCAT(贝福Beckhoff)基础教程5.1 TwinCAT-1 获取和设置系统时间
使用功能块NT_GetTime,NETID填写两个单引号表示本机,START就是一个触发信号,一般的功能块都需要一个上升沿触发执行,最后的输出类型都是让系统自己决定,然后统一把这些变量放到全局变量中( ...
- Linux非阻塞IO(六)使用poll实现非阻塞的服务器端
关于poll模型监听的事件以及返回事件,我们定义宏如下: #define kReadEvent (POLLIN | POLLPRI) #define kWriteEvent (POLLOUT | PO ...
- mysql开发之---使用游标双层嵌套对总表进行拆分为帖子表和回复表
注意点: (1)进行拆分的总表表名是不同的.所以创建暂时表,把总表的数据先插入暂时表 (2)为了避免最外层游标轮询数据结束时,抛出 not found 退出程序,不会运行关闭游标等兴许操作,定义con ...
- UINavigationController改变动画效果
@interface UINavigationController (CustomTransition) - (void) pushWithCustomAnimation:(UIViewControl ...
- AVCaptureSession
AVCaptureSession用来控制来自一个输入设备(AVCaptureDeviceInput)的声音和视频,流入一个输出缓冲区(AVCaptureOutput)的过程. 建立一个AVCaptur ...
- VBA 按列查找小工具类似lookUp函数
如上图,查找A列的数据在D,F列是否存在,如果存在背景色变绿,如果不存在则A列的背景色变红. 直接贴上代码: Private Sub CommandButton1_Click() Call lookU ...
- windows快捷启动命令
gpedit.msc-----组策略 sndrec32-----录音机 nslookup----- ip地址侦测器 explorer------ 打开资源管理器 logoff-------注销命令 t ...