缺省的情况下,通常见到Activity的标题栏(Titlebar)是这样的(红色框内):

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

1.首先如下创建一个Android项目

2.将图片magnifier.png拖入该项目的res/drawable-mdpi文件夹下。magnifier.png图片的样子是这样的:

3.在该项目的res/layout文件夹下,创建一个布局titlebar.xml,这个布局将用于定制Activity的标题栏

编辑titlebar.xml,使其内容如下:

<?xmlversion="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="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/magnifier"
android:gravity="bottom"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="38dip"
android:text="@string/app_name"
android:textColor="#FFFFFFFF"
android:textSize="14dip"
android:paddingTop="1dip"
/>
<EditText
android:id="@+id/searchparameter"
android:layout_width="wrap_content"
android:layout_height="38dip"
android:text="ABCDEFGHIJ"
android:textSize="14dip"
android:layout_margin="1dip"
/>
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="38dip"
android:text="OK"
android:textSize="14dip"
/>
</LinearLayout>

在上面的LinearLayout中,增加了以下控件:

一个ImageView,用于显示一个图标

一个TextView,用于显示应用的名称

一个EditText,用于接收输入

一个Button,用于测试

4.修改CustomizeTitlebar.java,使之如下:

public class CustomizeTitlebar extends Activity
{
@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.titlebar);
}
}

以上加粗的两行很重要,而且必须要严格按照上面那样的顺序出现在代码中。即:

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);必须出现在super.onCreate(savedInstanceState);之后,setContentView(R.layout.main);之前。其意思就是告诉系统,本程序要自己定义Titlebar;

getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.titlebar); 则必须出现在setContentView之后,其意思就是告诉系统,自定义的布局是R.layout.titlebar(即,我们前面编写的titlebar.xml)

到这里,不妨来运行一下,看看结果如何:

我们看到,Titlebar基本上按照我们的意思进行了改变,但也存在着一个缺陷:Titlebar太窄了,那么怎样改变Titlebar的高度呢?

5. 要改变Titlebar的高度,我们得先创建styles.xml(目录一般在/res/values/下),编辑styles.xml,使其内容如下:

<?xmlversion="1.0" encoding="utf-8"?>
<resources>
<style name="titlebarstyle"parent="android:Theme">
<item name="android:windowTitleSize">38dip</item>
</style>
</resources>

上面<item name="android:windowTitleSize">39dip</item>这一句,就是用来设定Titlebar的高度的。

6.在上面的基础上,我们需要修改AndroidManifest.xml中,相应Activity的属性。如下:

<?xmlversion="1.0" encoding="utf-8"?>
<manifestxmlns:android="http://schemas.android.com/apk/res/android"
package="com.pat.customizetitlebar"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon"android:label="@string/app_name">
<activity android:name=".CustomizeTitlebar"
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>
<uses-sdk android:minSdkVersion="8"/>
</manifest>

注意粗体字是新增上去的,其中的titlebar是在第5步中增加的。现在来看看运行结果:

可以看到结果完全符合了我们的要求。

7.我们还可以改变Titlebar的背景颜色。为此我们修改前面的styles.xml,使之如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomizedWindowTitleBackgroundColor">
<item name="android:background">#047BF0</item>
</style>
<style name="titlebarstyle" parent="android:Theme">
<item name="android:windowTitleSize">38dip</item>
<item name="android:windowTitleBackgroundStyle">@style/CustomizedWindowTitleBackgroundColor</item>
</style>
</resources>

注意,其中的粗体字是新增加的。

项目其他文件,均无需变动。运行结果如下:

8.最后,我们以OK按钮为例来测试Titlebar上的控件的事件响应。为此,修改CustomizeTitlebar.java,使之如下:

 public class CustomizeTitlebar extends Activity implements OnClickListener
{
private Button button;
@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.titlebar);
button = (Button)findViewById(R.id.button);
button.setOnClickListener(this);
}
public voidonClick(View v)
{
if(v.getId() == R.id.button)
{
Toast.makeText(this, "OK button in Titlebar clicked...", Toast.LENGTH_LONG).show();
}
}
}

粗体字部分是新增加的代码。重新运行本项目,等界面出来后,点击Titlebar上的OK按钮,将出现:

这说明,Titlebar上自己增加上去的控件,可以很好地响应相关的事件。

来源: http://blog.csdn.net/pathuang68/article/details/6646792

Android 自定义Activity的标题栏(Titlebar)的更多相关文章

  1. 自定义 Activity 的 标题栏 TitleBar

    自定义 Activity 的 标题栏 TitleBar 1. 修改标题栏的高度,背景 编辑styles.xml,添加: <?xmlversion="1.0" encoding ...

  2. Android自定义实现微信标题栏

    Android自定义实现微信标题栏     前言:在android的开发中有时我们需要更个性化的标题栏,而不仅仅是系统预定义的图标加软件名,同时有时候我们需要在标题栏中实现更多功能,如添加按钮响应用户 ...

  3. android自定义Activity窗体大小

    先给大家看图吧: 看,是不是很酷呢,呵呵. 这里我说关键的地方,就是自定义Activity的窗体大小. 这个登录框它不是一个Dialog,而是一个Activity. 如何定义,即把Activity的主 ...

  4. Android 自定义Activity基类与TitleBar

    我们在开发App的时候有时候碰到多个界面有一个共同点的时候,比如,都有相同的TitleBar,并且TitleBar可以设置显示的文字.TitleBar上的点击事件,如果给每一个Activity都写一遍 ...

  5. android自定义activity

    今天公司有个需要需要自动弹出界面,而dialog又不符合要求,所以自定义的一个activity的样式 首先在androidmainfest.xml上注册你的activity <activity ...

  6. Android自定义Activity酷炫的动画跳转效果

    两个Activity跳转的时候,自定义翻页效果: Intent intent = new Intent(FirstActivity.this, SecondActivity.class);   sta ...

  7. android自定义Activity窗口大小(theme运用)

    http://gundumw100.iteye.com/blog/906195 正常情况下,我们开发的应用程序都会上占满整个屏幕,那么怎么样才能开发出自定义窗口大小的的程序呢?如下图所示: 实现起来非 ...

  8. Android 自定义Activity栈对Activity统一管理

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6307239.html public class AppManager { private static St ...

  9. [置顶] xamarin android自定义标题栏(自定义属性、回调事件)

    自定义控件的基本要求 这篇文章就当是自定义控件入门,看了几篇android关于自定义控件的文章,了解了一下,android自定义控件主要有3种方式: 自绘控件:继承View类,所展示的内容在OnDra ...

随机推荐

  1. nodebb在阿里云主机部署过程

    1.在centos上安装nodejswget http://nodejs.org/dist/v0.8.9/node-v0.8.9.tar.gztar zxvf node-v0.8.9.tar.gzcd ...

  2. windows server 2012 iis8.0部署mvc报错

    一开始以为需要在服务器装mvc在很多论坛找过也问了朋友都说需要装mvc,经过两天研究是不需要装mvc的只需要在项目的bin文件夹下放入下面三个dll. 未能加载文件或程序集“System.Web.Ht ...

  3. ipython与python的区别

    http://mba.shengwushibie.com/itbook/BookChapter.asp?id=8745 http://www.cnblogs.com/yangze/archive/20 ...

  4. 开源中国iOS客户端学习

    开源中国iOS客户端学习 续写前言 <开源中国iOS客户端学习>续写前系列博客    http://blog.csdn.net/column/details/xfzl-kykhd.html ...

  5. Explain语法

    EXPLAIN SELECT -- 变体: 1. EXPLAIN EXTENDED SELECT -- 将执行计划"反编译"成SELECT语句,运行SHOW WARNINGS 可得 ...

  6. python 单元测试

    http://blog.csdn.net/five3/article/details/7104466

  7. SharePoint2013 Powershell script to get site Title, Site Owner, Site user count and usage

    Powershell script to get site Title, Site Owner, Site user count and usage Add-PSSnapin microsoft.sh ...

  8. PYTHON之批量文件指定字符串替换

    在工作应用中,运维自动化的基础是标准化. 而标准化的工作,是难点,在公司相关部门的配合. 那么,在有标准化之后,相应的部署脚本,就比较好写了. 贡献一个在类似环境下可以运用的东东.. 当然,可以写得更 ...

  9. 14.5.3 Locks Set by Different SQL Statements in InnoDB

    14.5.3 Locks Set by Different SQL Statements in InnoDB 通过不同的SQL语句设置的锁 在InnoDB中 一个锁定读, 一个UPDATE 或者一个D ...

  10. cscope使用

    [[]][]再加上][一共是 4 个在段落(对于 C 来讲就是函数)间跳转的命令. 总结是:1,相同就跳到函数的开头:(如果都是左括号或者都是右括号),不同就跳到函数的结尾:     { 和 } 用来 ...