自定义Activity标题栏(Title bar)和窗体显示状态操作(requestWindowFeature()的应用)
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_LEFT_ICON);
setContentView(R.layout.main);
getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,
android.R.drawable.icon);
// ...
}
< 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 >
< 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 >
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))+ "%");
}
};
}
< 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 >
< 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 >
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来隐藏
//...
}
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);
// ...
}