1. 標題欄顯示圖標
public
void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_LEFT_ICON);
setContentView(R.layout.main);
getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,
android.R.drawable.icon);
// ...
}
但實際效果呢,我以爲很差看,和旁邊的文字有至關距離!看看別人的圖片的:
固然這個圖標也能夠經過自定義佈局,使用ImageView來實現:
<?
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"
>
<
ImageView
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:src
="@drawable/icon"
/>
<
TextView
android:id
="@+id/text"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:layout_alignParentLeft
="true"
android:text
="文本"
/>
</
LinearLayout
>
效果圖:
2.自定義佈局
看看我自定義的標題欄:
佈局代碼(titlebar.xml)
<?
xml
version
="1.0"
encoding
="utf-8"
?>
<
LinearLayout
xmlns:android
="http://schemas.android.com/apk/res/android"
android:orientation
="horizontal"
android:layout_width
="fill_parent"
android:layout_height
="wrap_content"
>
<
TextView
android:text
="@string/app_name"
android:textColor
="#000"
android:paddingRight
="3.0dip"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
/>
<
TextView
android:text
="@string/battery_text"
android:textColor
="#000"
android:paddingRight
="3.0dip"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
/>
<
TextView
android:id
="@+id/battery_text"
android:textColor
="#00f"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
/>
</
LinearLayout
>
Java代碼:
public
void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
//自定義標題欄
mWindow = getWindow();
mWindow.setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.titlebar);
mBatteryText = (TextView)findViewById(R.id.battery_text);
mBatteryInforeceiver =
new BroadcastReceiver(){
@Override
public
void onReceive(Context context, Intent intent) {
int level = intent.getIntExtra(
"level", 0);
int scale = intent.getIntExtra(
"scale", 1);
mBatteryText.setText(String.valueOf((
int)(level*100/scale))+
"%");
}
};
}
你還能夠添加其餘控件,而這些控件的獲取和事件響應都是直接在activity裏面完成。
3. 設置標題欄的背景色和高度
雖然咱們能夠經過自定義佈局文件在標題欄加入一些控件,可是仍然不能改變標題欄的高度、背景色,要想達到這個目的,只能使用theme(主題)。
\res\values\style.xml:
<?
xml
version
="1.0"
encoding
="utf-8"
?>
<
resources
>
<
style
name
="CustomWindowTitleBackground"
>
<
item
name
="android:background"
>#47B2FF
</
item
>
</
style
>
<
style
name
="activityTitlebar"
parent
="android:Theme"
>
<
item
name
="android:windowTitleSize"
>34dp
</
item
> <!--
高度 -->
<
item
name
="android:windowTitleBackgroundStyle"
>@style/CustomWindowTitleBackground
</
item
> <!--
背景色,須要調用前面的顏色設置 -->
</
style
>
</
resources
>
窗體顯示狀態操做(requestWindowFeature()的應用)
首先介紹一個重要方法那就是requestWindowFeature(featrueId),它的功能是啓用窗體的擴展特性。參數是Window類中定義的常量。
1、枚舉常量
1.DEFAULT_FEATURES:系統默認狀態,通常不須要指定
2.FEATURE_CONTEXT_MENU:啓用ContextMenu,默認該項已啓用,通常無需指定
3.FEATURE_CUSTOM_TITLE:自定義標題。當須要自定義標題時必須指定。如:標題是一個按鈕時
4.FEATURE_INDETERMINATE_PROGRESS:不肯定的進度
5.FEATURE_LEFT_ICON:標題欄左側的圖標
6.FEATURE_NO_TITLE:沒標題
7.FEATURE_OPTIONS_PANEL:啓用「選項面板」功能,默認已啓用。
8.FEATURE_PROGRESS:進度指示器功能
9.FEATURE_RIGHT_ICON:標題欄右側的圖標
對於默認啓用的和前面有介紹的就略去不提了。咱們說比較經常使用的FEATURE_INDETERMINATE_PROGRESS和FEATURE_NO_TITLE。
FEATURE_INDETERMINATE_PROGRESS:表示一個進程正在運行
progress.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"
>
<
ProgressBar
android:id
="@+id/progress"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:layout_gravity
="center_vertical"
style
="?android:attr/progressBarStyleSmallTitle"
>
</
ProgressBar
>
</
LinearLayout
>
Java代碼
public
void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_INDETERMINATE_PROGRESS, R.layout.progress);
setProgressBarIndeterminateVisibility(
true);
//適當時候set false來隱藏
//...
}
標題進度條顯示
FEATURE_NO_TITLE 就是不顯示標題欄,某些時候全屏須要,但全屏不等於不顯示標題欄,我嘗試顯示標題欄的同時全屏來去掉系統的狀態欄:
Java代碼
public
void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
//自定義標題欄
mWindow = getWindow();
mWindow.setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.titlebar);
/* full screen */
mWindow.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// ...
}
因此真正實現全屏的是後面的那句話!
效果圖
***********