Android应用开发基础篇(14)-----自定义标题栏
来源:互联网 发布:tomcat nginx 编辑:程序博客网 时间:2024/05/01 15:15
一、概述
每一个应用程序默认的标题栏(注意与状态栏的区别)只有一行文字(新建工程时的名字),而且颜色、大小等都是固定的,给人的感觉比较单调。但当程序需要美化的时候,那么修改标题栏是就是其中一项内容,虽然Android已经定义了很多样式资源,但更多时候我们需要使用的是自己定义的样式。
二、要求
使用自己定义的样式来修改程序的标题栏。
三、实现
新建工程MyTitle,不用修改main.xml文件,在/res/layout目录下新建布局文件title.xml,在里面添加一个TextView和一个Button,完整的title.xml文件如下:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="match_parent" 5 android:orientation="horizontal" 6 > 7 8 <TextView 9 android:layout_width="wrap_content"10 android:layout_height="wrap_content"11 android:text="这是定制的标题栏"12 android:textStyle="bold"13 android:textColor="#FFFF0000" 14 />15 16 <Button 17 android:id="@+id/button"18 android:layout_width="wrap_content"19 android:layout_height="wrap_content"20 android:text="点我"21 />22 23 </LinearLayout>
在/res/values目录下新建titlestyle.xml文件,在里面定义两个style,一个用来修改标题栏的大小,一个用来修改标题栏的背景颜色,如下:
1 <?xml version="1.0" encoding="utf-8"?> 2 3 <resources> 4 5 <style name="TitleBackgroundColor"> 6 <item name="android:background">#FF0000FF</item> 7 </style> 8 9 <style name="titlestyle" parent="android:Theme" >10 <item name="android:windowTitleSize">40dip</item> 11 <item name="android:windowTitleBackgroundStyle">@style/TitleBackgroundColor</item>12 </style>13 14 </resources>
修改AndroidManifest.xml文件,在application标签下添加一行:
android:theme="@style/titlestyle"
最后,修改MyTitleActivity.java文件,设置使用自定义的标题栏,实现Button按钮的监听,如下:
1 package com.nan.title; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.view.View; 6 import android.view.Window; 7 import android.widget.Button; 8 import android.widget.Toast; 9 10 public class MyTitleActivity extends Activity 11 {12 private Button mButton = null;13 14 /** Called when the activity is first created. */15 @Override16 public void onCreate(Bundle savedInstanceState) 17 {18 super.onCreate(savedInstanceState);19 //使用自定义标题栏20 requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);21 setContentView(R.layout.main);22 //使用布局文件来定义标题栏23 getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);24 25 mButton = (Button)this.findViewById(R.id.button);26 //按钮监听27 mButton.setOnClickListener(new View.OnClickListener() 28 {29 30 @Override31 public void onClick(View v) 32 {33 // TODO Auto-generated method stub34 displayToast("Clicked!");35 }36 });37 38 }39 40 //显示Toast函数41 private void displayToast(String s)42 {43 Toast.makeText(this, s, Toast.LENGTH_SHORT).show();44 }45 46 }
注意上面程序的第20~23行的顺序不能调乱。
运行该程序:
点击一下“点我”按钮:
转自:http://www.cnblogs.com/lknlfy/archive/2012/03/08/2385724.html
另可参考:http://www.blogjava.net/Green-nut/articles/332617.html
- Android应用开发基础篇(14)-----自定义标题栏
- Android - 自定义标题栏(TitleBar)
- Android:自定义标题栏(titlebar)
- Android--自定义标题栏(TopBar)
- Android 开发之自定义标题栏、状态栏
- 一步一步开发安卓应用 自定义Activity标题栏
- 安卓开发-自定义标题栏(1)
- Android自定义状态栏颜色与应用标题栏颜色一致
- Android 自定义Activity标题栏之requestWindowFeature()的应用
- Android基础--ActionBar的基本用法,自定义View,标题栏
- Android应用去掉标题栏(Android Studio)
- Android 自定义标题栏(title栏)
- Android自定义title bar(标题栏)
- Android中自定义标题栏(一)
- Android中自定义标题栏(二)
- Android中自定义标题栏(三)
- Android 自定义标题栏(title栏)
- Android 自定义标题栏(title栏)
- JavaScrip中闭包概念的探讨
- android用于打开各种文件的intent
- jira 4.2配置oracle
- 精仿时尚女性网DEDE5.7模板下载GBK UTF8双版本
- CSS的优先级别
- Android应用开发基础篇(14)-----自定义标题栏
- loadrunner 三种post函数区别
- How to find if native dll is compiled as x64 or x86?
- 写在00100010岁
- html doctype 浅见
- 雕虫小技让Discuz利用WinwebMail发邮件
- win7使用笔记本做wifi热点
- LightHouse3D的GLSL教程
- CGI中表单信息的编码规则