Android程序窗体显示:requestWindowFeature()

本文围绕以下五个部分展开:

一、requestWindowFeature()
二、FEATURE_CUSTOM_TITLE:自定义标题
三、FEATURE_INDETERMINATE_PROGRESS
四、FEATURE_LEFT_ICON
五、FEATURE_NO_TITLE






一、requestWindowFeature()

        我们在开发Android应用程序时经常会需要软件全屏显示、自定义标题(使用按钮等控件)或其他的需求,因此需要掌握Android应用程序窗体显示的方法。

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





二、FEATURE_CUSTOM_TITLE:自定义标题

        // 自定义标题。当需要自定义标题时必须指定。如:标题是一个按钮时
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.activity_main);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.customtitle);


        customtitle.xml。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <ImageView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"/>
    <TextView android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="文本" />

</LinearLayout>






三、FEATURE_INDETERMINATE_PROGRESS

        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.activity_main);
        getWindow().setFeatureInt(Window.FEATURE_INDETERMINATE_PROGRESS, R.layout.progress);
        // 必须得加上否则显示不出效果 可以通过这个在以后设置显示或隐藏
        setProgressBarIndeterminateVisibility(true);


        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"
    android:orientation="vertical">

    <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>






四、FEATURE_LEFT_ICON

        requestWindowFeature(Window.FEATURE_LEFT_ICON);
        setContentView(R.layout.activity_main);
        getWindow().setFeatureInt(Window.FEATURE_LEFT_ICON, R.mipmap.ic_launcher);






五、FEATURE_NO_TITLE

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        // 加上这句设置为全屏 不加则只隐藏title  
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);









整理时参考:
http://www.cnblogs.com/salam/archive/2010/11/30/1892143.html
http://zhanhao.iteye.com/blog/1174914

猜你喜欢

转载自xiangdonglee.iteye.com/blog/2243013