您的位置:首页 > 移动开发 > Android开发

自定义Android 标题栏TitleBar布局

2010-12-27 16:57 393 查看
http://www.blogjava.net/Green-nut/articles/332617.html?opt=admin

自定义Android 标题栏TitleBar布局


多网友发现自己Android程序的标题栏TitleBar区域很单调,如果想个性化一些可以通过下面的方法来为自己软件的标题定制一个layout布局
文件,比如浏览器的标题栏,它包含了网站的Favicon,自定义的进度条,和不确定的进度指示等等,实现的方法自己控制吧。下面代码在onCreate
中使用,同时顺序不要改变,否则将无法生效:

<!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br />
<br />
-->requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

setContentView(R.layout.main);
//
软件activity的布局

getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar);
//
titlebar为自己标题栏的布局

这样虽然可以在一定程度上定制标题栏, 不过, 这里无法改变标题栏的高度和背景(背景设置之后会在两端有两个非常难看的边框). 据说, 原因是android 固有的.

这里有修改方法:

原理是这样的. 直接像上述代码那样添加title仅仅是把一个子界面添加到原有的title上的, 并没有改变原来的属性, 比如 标题栏大小, 标题栏背景. 这些需要在theme 主题里面定义.

因此先定义一个style, 若修改背景请修改android:windowTitleBackgroundStyle

若修改标题栏高度,请修改android:windowTitleSize

例子:

<!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br />
<br />
--><?
xml version="1.0" encoding="utf-8"
?>

<
resources
xmlns:android
="http://schemas.android.com/apk/res/android"
>

<
style
name
="CustomWindowTitleBackground"
>

<
item
name
="android:background"
>
#565656
</
item
>

</
style
>

<
style
name
="test"
parent
="android:Theme"
>

<
item
name
="android:windowTitleSize"
>
50dp
</
item
>

<
item
name
="android:windowTitleBackgroundStyle"
>
@style/CustomWindowTitleBackground
</
item
>

</
style
>

</
resources
>

在程序的android manifest.xml中对应activity中添加属性 android:theme = "@style/test" 就可以了

<!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br />
<br />
--><?
xml version="1.0" encoding="utf-8"
?>

<
manifest
xmlns:android
="http://schemas.android.com/apk/res/android"

package
="com.guardian"

android:versionCode
="1"

android:versionName
="1.0"
>

<
application
android:icon
="@drawable/icon"
android:label
="@string/app_name"

>

<
activity
android:name
=".Guardian"

android:label
="@string/app_name"

android:theme
= "@style/test" //就在这里

>

<
intent-filter
>

<
action
android:name
="android.intent.action.MAIN"

/>

<
category
android:name
="android.intent.category.LAUNCHER"

/>

</
intent-filter
>

</
activity
>

</
application
>

<
uses-sdk
android:minSdkVersion
="4"

/>

</
manifest
>

之后借助于设置自定义的标题栏xml文件,就可以自定义标题栏布局了



Android改变窗口标题栏的布局

一、

重点

一般应用的
Title

都是建立应用时在
AndroidManifest.xml

中配置的,或是用
setTitle

设置的简单字符串,要是想加入按钮,图片等多个复杂的布局,使用以下方法:

在窗口建立时,可以把一个
xml

布局设置成该应用的
Title

二、

实例

a)

功能:把
title

设置成为一个字串和一个按钮的组合

b)

修改
xxActivity.java

代码

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.title);

}



c)

填加
title.xml

代码

<?xml version="1.0"
encoding="utf-8"
?>

<LinearLayout

xmlns:android= http://schemas.android.com/apk/res/android
android:layout_width=
"wrap_content"

android:layout_height="wrap_content"
>

<TextView android:id="@+id/text"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentLeft="true"

android:text="text"
/>

<Button android:id="@+id/button"

android:layout_width="wrap_content"

android:layout_height="30px"

android:text="button"
/>

</LinearLayout>



三、

注意

a)

注意设置顺序

requestWindowFeature

要在
setContentView

之前

getWindow().setFeatureInit

最好在
setContentView

之后

b)

注意
requestWindowFeature(
Window.FEATURE_CUSTOM_TITLE
)

不要和其它对
TITLE

的设置
requestWindowFeature(xxxx)

一起使用

http://xy0811.spaces.live.com/blog/cns!F8AECD2A067A6B17!1375.entry
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: