关于requestWindowFeature用法以及出现的问题

requestWindowFeature 的用法往往出现在设置布局前,setContentView方法前的,这是重点一定要注意他必须放在setContentView 方法前、


我们可以从源码来解释下,requestWindows 方法其实走的是requestFeature 路线


 @Override
    public boolean requestFeature(int featureId) {
        if (mContentParent != null) {
            throw new AndroidRuntimeException("requestFeature() must be called before adding content");
        }

        .....

        return super.requestFeature(featureId);
    }


但是在activity中使用setContentView()时,实际上走的是phonewindow的setContentView,根据代码我们看到


    @Override

    public void setContentView(View view, ViewGroup.LayoutParams params) {
        if (mContentParent == null) {
            installDecor();
        } else {
            mContentParent.removeAllViews();
        }
        mContentParent.addView(view, params);
        final Callback cb = getCallback();
        if (cb != null && !isDestroyed()) {
            cb.onContentChanged();
        }

    }

它会首先判断mContentParent 是否为null,如果为null,进入installDecor();

private void installDecor() {

......

if (mContentParent == null) {
            mContentParent = generateLayout(mDecor);


            mTitleView = (TextView)findViewById(com.android.internal.R.id.title);
            if (mTitleView != null) {
                if ((getLocalFeatures() & (1 << FEATURE_NO_TITLE)) != 0) {
                    View titleContainer = findViewById(com.android.internal.R.id.title_container);
                    if (titleContainer != null) {
                        titleContainer.setVisibility(View.GONE);
                    } else {
                        mTitleView.setVisibility(View.GONE);
                    }
                    if (mContentParent instanceof FrameLayout) {
                        ((FrameLayout)mContentParent).setForeground(null);
                    }
                } else {
                    mTitleView.setText(mTitle);
                }
            }

.....

}

这是installDecor方法的部分代码,从中我们可以看到,它会对mContentParent 进行初始化,从而赋予相应的值,这就是RequestWindowFeature为啥一定要在setContentView之前调用就会抛此类异常

然后我们看下用法:

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

二、详解

默认显示状态

 

1.FEATURE_CUSTOM_TITLE详解

Java代码   收藏代码
  1. this.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);  
  2. setContentView(R.layout.main);  
 

这是因为没有设置Featrue

在上面代码后加:getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.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" >  
  
    <ProgressBar  
        android:id="@+id/progress"  
        style="?android:attr/progressBarStyleSmallTitle"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_gravity="center_vertical" >  
    </ProgressBar>  
  
</LinearLayout>  

2.FEATURE_INDETERMINATE_PROGRESS详解

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

Java代码   收藏代码
  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);  
 
Xml代码   收藏代码
  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.   
  6.     <ProgressBar  
  7.         android:id="@+id/progress"  
  8.         style="?android:attr/progressBarStyleSmallTitle"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_gravity="center_vertical" >  
  12.     </ProgressBar>  
  13.   
  14. </LinearLayout>  
  

3.FEATURE_LEFT_ICON和FEATURE_RIGHT_ICON详解

Java代码   收藏代码
  1. requestWindowFeature(Window.FEATURE_RIGHT_ICON);  
  2. setContentView(R.layout.main);      
  3. getWindow().setFeatureDrawableResource(Window.FEATURE_RIGHT_ICON,R.drawable.ic_launcher);  
 
Java代码   收藏代码
  1. requestWindowFeature(Window.FEATURE_LEFT_ICON);  
  2. setContentView(R.layout.main);          
  3. getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,R.drawable.ic_launcher);  
 

 

 4.FEATURE_NO_TITLE详解

Java代码   收藏代码
  1. this.requestWindowFeature(Window.FEATURE_NO_TITLE);  
  2. setContentView(R.layout.main);  
 
Java代码   收藏代码
  1. getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);  
 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值