android自定义标题栏
2013-12-16 21:56
246 查看
MainActivity代码
package com.clyao.title;
import android.os.Bundle;
import android.view.Window;
import android.app.Activity;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); // 声明使用自定义标题
setContentView(R.layout.activity_main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
R.layout.title_main);// 自定义布局赋值
}
}
xml代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:contentDescription="@string/hello_world"
android:src="@drawable/back" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/title"
android:layout_centerInParent="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:text="@string/add" />
</RelativeLayout>
Strings代码
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">自定义标题栏</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="returns">返回</string>
<string name="add">新增</string>
<string name="title">自定义标题栏</string>
</resources>
styles代码
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
<!-- 自定义一个 -->
<style name="CustomWindowTitleBackground">
<item name="android:background">#f70250</item>
</style>
<!-- 自定义一个标题栏 -->
<style name="test" parent="android:Theme">
<item name="android:windowTitleSize">40dp</item>
<item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>
</style>
</resources>
运行结果:
package com.clyao.title;
import android.os.Bundle;
import android.view.Window;
import android.app.Activity;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); // 声明使用自定义标题
setContentView(R.layout.activity_main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
R.layout.title_main);// 自定义布局赋值
}
}
xml代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:contentDescription="@string/hello_world"
android:src="@drawable/back" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/title"
android:layout_centerInParent="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:text="@string/add" />
</RelativeLayout>
Strings代码
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">自定义标题栏</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="returns">返回</string>
<string name="add">新增</string>
<string name="title">自定义标题栏</string>
</resources>
styles代码
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
<!-- 自定义一个 -->
<style name="CustomWindowTitleBackground">
<item name="android:background">#f70250</item>
</style>
<!-- 自定义一个标题栏 -->
<style name="test" parent="android:Theme">
<item name="android:windowTitleSize">40dp</item>
<item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>
</style>
</resources>
运行结果:
相关文章推荐
- Android 完全自定义对话框的实现(标题栏+EditText+双按钮)
- Android 功能最强的自定义标题栏控件
- 【Android UI设计与开发】第13期:顶部标题栏(四)自定义ActionBar风格和样式 .
- Android自定义view----顶部标题栏2
- Android自定义标题栏后,刚进入应用会出现黑色的标题背景栏的解决方案
- Android自定义标题栏
- 解决android自定义标题栏充满的问题
- Android自定义标题栏
- Android随笔之标题栏的去除以及自定义
- 自定义android标题栏TitleBar
- Android应用开发基础篇(14)-----自定义标题栏
- android_actionbar自定义标题栏
- Android在activity页面中让标题栏全屏显示自定义颜色
- android实现自定义标题栏
- android 自定义标题栏 titleBar自定义
- Android:自定义标题栏
- Android开发之——自定义标题栏titlebar
- 自定义Android标题栏修改TitleBar的布局
- Android项目中自定义顶部标题栏
- 解决android自定义标题栏充满的问题