I recently saw an article on the Internet about how to use requestwindowfeature () in Android windows.
Requestwindowfeature (window. feature_left_icon );
Setcontentview (R. layout. dialog_activity );
Getwindow (). setfeaturedrawableresource (window. feature_left_icon, Android. R. drawable. ic_dialog_alert );
Android Application Form display status operation (Application of requestwindowfeature)
During development, full screen display, custom titles (using buttons and other controls), and other requirements are often required. Today we will talk about how to control the form display of Android applications.
First, an important method is introduced.Requestwindowfeature (featrueid ),Its function is to enable extended form features. A parameter is a constant defined in the window class.
I. Enumeration Constants
1. default_features: default system status, which is not required
2. feature_context_menu: Enable contextmenu. This option is enabled by default. Generally, you do not need to specify
3. feature_custom_title: Custom title. 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 on the left of the title bar
6. 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
Ii. Details
1. Default display status
2. feature_custom_title
This. requestwindowfeature (window. feature_custom_title );
Setcontentview (R. layout. Main );
This is because featrue is not set.
After the code above, add: getwindow (). setfeatureint (window. feature_custom_title, R. layout. Title );
The custom title is complete. It is an XML file layout.
Title. 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">
<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>
3. feature_indeterminate_progress
Indicates that a process is running.
Implementation Code
1. 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"
>
</Progressbar>
</Linearlayout>
2.Java code
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
Icon displayed on the left
Implementation Code
This. requestwindowfeature (window. feature_left_icon );
Setcontentview (R. layout. Main );
Getwindow (). setfeaturedrawableresource (window. feature_left_icon, R. drawable. Icon );
5. feature_no_title
Can be used for Full Screen Display
Implementation Code
This. requestwindowfeature (window. feature_no_title );
Setcontentview (R. layout. Main );
Getwindow (). setflags (windowmanager. layoutparams. flag_fullscreen, windowmanager. layoutparams. flag_fullscreen );
Although some controls can be added to the title bar, the height and background color of the title bar cannot be changed. To achieve this goal, you can only use theme (theme ). Therefore, add a style to the project. Change the background color to modify the value of Android: windowtitlebackgroundstyle, and change the title bar height to change the value of Android: windowtitlesize. The following is an example:
<? XML version = "1.0" encoding = "UTF-8"?>
<Resources>
<Style name = "customwindowtitlebackground">
<Item name = "Android: background" >#778899 </item>
</Style>
<Style name = "activitytitlebar" parent = "Android: Theme">
<Item name = "Android: windowtitlesize"> 32dp </item>
<Item name = "Android: windowtitlebackgroundstyle"> @ style/customwindowtitlebackground </item>
</Style>
</Resources>
Next, modify the androidmanifest. xml file, find the activity to customize the title bar, and add the Android: Theme value, for example:
<Activity Android: Name = ". mainactivity" Android: theme = "@ style/activitytitlebar">
Android: The theme value is the name value of a style defined in the above style. xml file.
Follow these steps to modify the title bar layout, height, and background color.