0
点赞
收藏
分享

微信扫一扫

Android requestWindowFeature()的应用


android开发中经常会在setContentView(R.layout.XXX); 前设置requestWindowFeature(XXXX)。

他的意思是需要软件全屏显示、自定义标题(使用按钮等控件)和其他的需求

首先介绍一个重要方法那就是requestWindowFeature(featrueId),它的功能是启用窗体的扩展特性。参数是Window类中定义的常量。

一、枚举常量

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:标题栏右侧的图标

二、详解

默认显示状态

Android requestWindowFeature()的应用_java代码


1.FEATURE_CUSTOM_TITLE详解


Java代码   Android requestWindowFeature()的应用_ico_02



  1. this.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);  
  2. setContentView(R.layout.main);  

 Android requestWindowFeature()的应用_android_03


这是因为没有设置Featrue

在上面代码后加:getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);


Android requestWindowFeature()的应用_requestWindowFeature_04

自定义标题完成,它是一个xml文件布局



Xml代码   Android requestWindowFeature()的应用_ico_02



  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="wrap_content"  
  4.     android:layout_height="wrap_content" >  

  5.     <ImageView  
  6.         android:layout_width="wrap_content"  
  7.         android:layout_height="wrap_content"  
  8.         android:src="@drawable/ic_launcher"  
  9.         />  

  10.     <TextView  
  11.         android:id="@+id/text"  
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:layout_alignParentLeft="true"  
  15.         android:textColor="#000000"  
  16.         android:text="FEATURE_CUSTOM_TITLE" />  

  17. </LinearLayout>  



2.FEATURE_INDETERMINATE_PROGRESS详解

可以用来表示一个进程正在运行


Java代码   Android requestWindowFeature()的应用_ico_02



  1. this.requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);  
  2. setContentView(R.layout.main);  
  3. getWindow().setFeatureInt(Window.FEATURE_INDETERMINATE_PROGRESS, R.layout.progress);  
  4. setProgressBarIndeterminateVisibility(true);  

 Android requestWindowFeature()的应用_android_07

Xml代码   Android requestWindowFeature()的应用_ico_02



  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="wrap_content"  
  4.     android:layout_height="wrap_content" >  

  5.     <ProgressBar  
  6.         android:id="@+id/progress"  
  7.         style="?android:attr/progressBarStyleSmallTitle"  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_gravity="center_vertical" >  
  11.     </ProgressBar>  

  12. </LinearLayout>  


3.FEATURE_LEFT_ICON和FEATURE_RIGHT_ICON详解


Java代码   Android requestWindowFeature()的应用_ico_02



  1. requestWindowFeature(Window.FEATURE_RIGHT_ICON);  
  2. setContentView(R.layout.main);      
  3. getWindow().setFeatureDrawableResource(Window.FEATURE_RIGHT_ICON,R.drawable.ic_launcher);  

  Java代码   Android requestWindowFeature()的应用_ico_02



  1. requestWindowFeature(Window.FEATURE_LEFT_ICON);  
  2. setContentView(R.layout.main);          
  3. getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,R.drawable.ic_launcher);  


Android requestWindowFeature()的应用_ico_11

Android requestWindowFeature()的应用_requestWindowFeature_12


 4.FEATURE_NO_TITLE详解


Java代码   Android requestWindowFeature()的应用_ico_02



  1. this.requestWindowFeature(Window.FEATURE_NO_TITLE);  
  2. setContentView(R.layout.main);  

 Android requestWindowFeature()的应用_java代码_14

Java代码   Android requestWindowFeature()的应用_ico_02


  1. getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);  

 Android requestWindowFeature()的应用_ico_16


举报

相关推荐

0 条评论