Application of the requestWindowFeature () operation for customizing the Title bar of an Activity and the display status of a form

Source: Internet
Author: User

1. display icon in the title barPublic 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 );
//...
} But what is the actual effect? I don't think it looks good. It's quite a distance from the text next to it! Look at other people's pictures: 650) this. width = 650; "src =" ../attachment/201108/172944760 .png" border = "0" alt = ""/>
Of course, this icon can also be implemented through custom layout and 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 = "text"/>
</LinearLayout>: 650) this. width = 650; "src =" ../attachment/201108/163821751 .png" border = "0" alt = ""/>
2. custom LayoutCheck out my custom title bar: 650) this. width = 650; "src = ".. /attachment/201108/155802165 .png" border = "0" alt = ""/> layout code 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 code: public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );

RequestWindowFeature (Window. FEATURE_CUSTOM_TITLE );

SetContentView (R. layout. main );

// Customize the title bar
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) + "% ");
}

};
} You can also add other controls. The acquisition and Event Response of these controls are directly completed in the activity.
3. Set the background color and height of the title bar.Although we can add some controls to the title bar through a custom Layout file, we still cannot change the height and background color of the title bar. To achieve this, we can only use 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> <! -- Height -->
<Item name = "android: windowTitleBackgroundStyle"> @ style/CustomWindowTitleBackground </item> <! -- Background color. You must call the preceding color settings -->
</Style>
</Resources>

Application of requestWindowFeature () in Form display status Operation)
First, we will introduce an important method: requestWindowFeature (featrueId). Its function is to enable extended form features. A parameter is a constant defined in the Window class. 1. Enumeration constant 1. DEFAULT_FEATURES: default system status. Generally, you do not need to specify 2. FEATURE_CONTEXT_MENU: Enable ContextMenu. By default, this item is enabled. Generally, 3. FEATURE_CUSTOM_TITLE: Custom title is not required. You must specify a custom title. For example, when the title is a button, 4. FEATURE_INDETERMINATE_PROGRESS: uncertain progress 5. FEATURE_LEFT_ICON: icon 6 on the left of the title bar. FEATURE_NO_TITLE: No title. 7. FEATURE_OPTIONS_PANEL: enables the option panel function. It is enabled by default. 8. FEATURE_PROGRESS: Progress indicator function 9. FEATURE_RIGHT_ICON: icon on the right of the title bar
If it is enabled by default and previously described, it will be omitted. FEATURE_INDETERMINATE_PROGRESS and FEATURE_NO_TITLE are commonly used.
FEATURE_INDETERMINATE_PROGRESS: indicates that a process is running.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 code 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 when appropriate to hide

//...
}
The title progress bar shows 650) this. width = 650; "src =" ../attachment/201108/180226404 .png" border = "0" alt = ""/>
FEATURE_NO_TITLEThe title bar is not displayed. In some cases, full screen is required, but full screen is not equal to the title bar. I try to display the title bar while full screen to remove the system's status bar: Java code public void onCreate (Bundle savedInstanceState) {
Super. onCreate (savedInstanceState );

RequestWindowFeature (Window. FEATURE_CUSTOM_TITLE );

SetContentView (R. layout. main );

// Customize the title bar
MWindow = getWindow ();
MWindow. setFeatureInt (Window. FEATURE_CUSTOM_TITLE, R. layout. titlebar );

/* Full screen */
MWindow. setFlags (WindowManager. LayoutParams. FLAG_FULLSCREEN,
WindowManager. LayoutParams. FLAG_FULLSCREEN );

//...
} So what really implements full screen is the following sentence!
650) this. width = 650; "src =" ../attachment/201108/180836825 .png" border = "0" alt = ""/>
* This part of content extracted from the Android Application Form display status operation requestWindowFeature () Application) http://www.cnblogs.com/salam/archive/2010/11/30/1892143.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.