android 标题样式,Android定义各种样式的标题栏:requestWindowFeature()
最近在網(wǎng)上看到一篇介紹Android window的requestWindowFeature()的使用方法,共享出來大家學(xué)習(xí)學(xué)習(xí)
requestWindowFeature(Window.FEATURE_LEFT_ICON);
setContentView(R.layout.dialog_activity);
getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, android.R.drawable.ic_dialog_alert);
Android 應(yīng)用程序窗體顯示狀態(tài)操作(requestWindowFeature()的應(yīng)用)
我們在開發(fā)程序是經(jīng)常會需要軟件全屏顯示、自定義標(biāo)題(使用按鈕等控件)和其他的需求,今天這一講就是如何控制Android應(yīng)用程序的窗體顯示.
首先介紹一個重要方法那就是requestWindowFeature(featrueId),它的功能是啟用窗體的擴展特性。參數(shù)是Window類中定義的常量。
一、枚舉常量
1.DEFAULT_FEATURES:系統(tǒng)默認狀態(tài),一般不需要指定
2.FEATURE_CONTEXT_MENU:啟用ContextMenu,默認該項已啟用,一般無需指定
3.FEATURE_CUSTOM_TITLE:自定義標(biāo)題。當(dāng)需要自定義標(biāo)題時必須指定。如:標(biāo)題是一個按鈕時
4.FEATURE_INDETERMINATE_PROGRESS:不確定的進度
5.FEATURE_LEFT_ICON:標(biāo)題欄左側(cè)的圖標(biāo)
6.FEATURE_NO_TITLE:無標(biāo)題
7.FEATURE_OPTIONS_PANEL:啟用“選項面板”功能,默認已啟用。
8.FEATURE_PROGRESS:進度指示器功能
9.FEATURE_RIGHT_ICON:標(biāo)題欄右側(cè)的圖標(biāo)
二、詳解1.默認顯示狀態(tài)
2.FEATURE_CUSTOM_TITLE詳解
this.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
這是因為沒設(shè)置Featrue
在上面代碼后加:getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);
自定義標(biāo)題完成,它是一個xml文件布局
title.xml
xmlversion="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">
<<>ImageViewandroid:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon"/>
<<>TextViewandroid:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="文本"/>
3.FEATURE_INDETERMINATE_PROGRESS詳解
表示一個進程正在運行
實現(xiàn)代碼
1.progress.xml
xmlversion="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">
<<>ProgressBarandroid:id="@+id/progress"
android:layout_width="wrap_content"android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
style="?android:attr/progressBarStyleSmallTitle">
2.Java代碼
this.requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_INDETERMINATE_PROGRESS, R.layout.progress);
setProgressBarIndeterminateVisibility(true);this.requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_INDETERMINATE_PROGRESS, R.layout.progress);
setProgressBarIndeterminateVisibility(true);
4.FEATURE_LEFT_ICON詳解
左側(cè)顯示圖標(biāo)
實現(xiàn)代碼
this.requestWindowFeature(Window.FEATURE_LEFT_ICON);
setContentView(R.layout.main);
getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.icon);this.requestWindowFeature(Window.FEATURE_LEFT_ICON);
setContentView(R.layout.main);
getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.icon);
5.FEATURE_NO_TITLE詳解
可用于全屏顯示
實現(xiàn)代碼
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
雖然上面這樣可以在標(biāo)題欄加入一些控件,但是仍然不能改變標(biāo)題欄的高度、背景色,要想達到這個目的,只能使用theme(主題)。因此往project里先添加一個style。改變背景色修改android:windowTitleBackgroundStyle的值,改變標(biāo)題欄高度則修改android:windowTitleSize的值。下面是一個示例:
xmlversion="1.0"encoding="utf-8"?>
#778899
32dp
@style/CustomWindowTitleBackground
接著再修改AndroidManifest.xml文件,找到要自定義標(biāo)題欄的Activity,添加上android:theme值,比如:
android:theme值就是上面那個style.xml文件里定義的一個style的name值。按照以上的步驟,修改標(biāo)題欄布局、高度、背景色的功能就實現(xiàn)了。xmlversion="1.0"encoding="utf-8"?>
<<>resources>
<<>stylename="CustomWindowTitleBackground">
<<>itemname="android:background">#778899
<<>stylename="activityTitlebar"parent="android:Theme">
<<>itemname="android:windowTitleSize">32dp
<<>itemname="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground
接著再修改AndroidManifest.xml文件,找到要自定義標(biāo)題欄的Activity,添加上android:theme值,比如:
android:theme值就是上面那個style.xml文件里定義的一個style的name值。
按照以上的步驟,修改標(biāo)題欄布局、高度、背景色的功能就實現(xiàn)了。
總結(jié)
以上是生活随笔為你收集整理的android 标题样式,Android定义各种样式的标题栏:requestWindowFeature()的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php调用python绘图程序_如何在m
- 下一篇: c语言程序设计平时作业代刷,C语言程序设