您的位置:首页 > 移动开发 > Android开发

Android 03:如何控制Android应用程序的窗体显示.

2015-08-06 11:36 483 查看
通常手机上的App打开后,该应用程序是全屏显示或者是自定义的标题、按钮,而我刚刚开始接触Android应用程序开发时,默认情况下是有标题栏的。今天就和大家一起分享下如何控制Android应用程序的窗体显示

  首先介绍一个重要方法那就是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:标题栏右侧的图标

二、详解

1.FEATURE_CUSTOM_TITLE详解

FEATURE_CUSTOM_TITLE:自定义标题。当需要自定义标题时必须指定。如:标题是一个按钮时

this.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);


title.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="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="这是自定义标题设置"
android:textColor="#ffffff"
android:layout_gravity="center"
android:layout_alignParentLeft="true"
android:textSize="12sp"/>

</LinearLayout>


运行之后可能会出现错误:

You cannot combine custom titles with other title features

这是因为一个activity设置了两个title,解决的方法:

在manifest文件中,修改android:theme=”@style/android:Theme.Light”

效果如图:



2.FEATURE_INDETERMINATE_PROGRESS详解

FEATURE_INDETERMINATE_PROGRESS:一般用来表示一个进度正在进行

this.requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_INDETERMINATE_PROGRESS, R.layout.title);
setProgressBarIndeterminateVisibility(true);


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>


效果如图:



3.FEATURE_LEFT_ICON和FEATURE_RIGHT_ICON详解

标题栏左侧/右侧的图标

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


效果如图:



4.FEATURE_NO_TITLE详解

无标题

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


效果:



this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);


全屏效果:



上述代码运行之后可能会出现错误:

You cannot combine custom titles with other title features

这是因为一个activity设置了两个title,解决的方法:

在manifest文件中,修改android:theme=”@style/android:Theme.Light”
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: