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

Android窗体自定义标题栏

2012-01-16 15:31 267 查看
自定义实现功能图片如下:



Java代码

package com.easyway.titlebar;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
/**
* 自定义窗体标签的样式表格式的使用
* 1.设置window标题信息
* requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); //声明使用自定义标题
* setContentView(R.layout.main);
* //设置窗体样式
* getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);//自定义布局赋值
* 2.在对应的Activity中添加相关的 android:theme="@style/test"管理对应的样式
*
* <activity android:name=".MainActivity"
* android:theme="@style/test">
* <intent-filter>
* <action android:name="android.intent.action.MAIN" />
* <category android:name="android.intent.category.LAUNCHER" />
* </intent-filter>
* </activity>
*
* @author longgangbai
*
*/
public class AndroidTitleBarActivity 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.title //设置对应的布局
);//自定义布局赋值
}
}

strings.xml

Java代码

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

<string name="hello">Hello World, AndroidTitleBarActivity!</string>
<string name="app_name">AndroidTitleBar</string>
<style name="CustomWindowTitleBackground">
<item name="android:background">@drawable/logo</item>
</style>

<style name="test" parent="android:Theme" mce_bogus="1">
<item name="android:windowTitleSize">40dp</item>
<item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>
</style>
</resources>

title.xml

Java代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<ImageView android:layout_width="wrap_content"
android:layout_centerVertical="true"
android:layout_height="wrap_content"
android:src="@drawable/qq" />
<TextView android:layout_width="wrap_content"
android:layout_centerInParent="true"
android:layout_height="wrap_content"
android:text="自定义标题栏" />

</RelativeLayout>

Java代码

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.easyway.titlebar"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<!--
主题信息定义在values/strings.xml文件中
android:theme="@style/test"
-->
<activity
android:label="@string/app_name"
android:theme="@style/test"
android:name=".AndroidTitleBarActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

AndroidTitleBar.rar (57.4 KB)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: