requestWindowFeature在网上找了一些但是用了之后都稍微也一些不足,在这里集合一些资料提供一个运行各功能正常的,下面就介绍下:
requestWindowFeature可以设置的值有:
// 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:标题栏右侧的图标
主Activity代码:
import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.Window; import android.view.WindowManager; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // requestWindowFeature();的取值 // 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:标题栏右侧的图标 /** FEATURE_INDETERMINATE_PROGRESS:不确定的进度 */ // showFEATURE_INDETERMINATE_PROGRESS(); /** FEATURE_CUSTOM_TITLE:自定义标题 */ // showFEATURE_CUSTOM_TITLE(); /** FEATURE_LEFT_ICON:标题栏左侧的图标 */ // showFEATURE_LEFT_ICON(); /** FEATURE_NO_TITLE:无标题 */ // showFEATURE_NO_TITLE(); /** FEATURE_PROGRESS:进度条 */ showFEATURE_PROGRESS(); } private void showFEATURE_INDETERMINATE_PROGRESS() { requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); setContentView(R.layout.activity_main); getWindow().setFeatureInt(Window.FEATURE_INDETERMINATE_PROGRESS, R.layout.progress); // 必须得加上否则显示不出效果 可以通过这个在以后设置显示或隐藏 setProgressBarIndeterminateVisibility(true); } private void showFEATURE_CUSTOM_TITLE() { // 自定义标题,当需要自定义标题时必须指定 requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); setContentView(R.layout.activity_main); getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.customtitle); } private void showFEATURE_LEFT_ICON() { requestWindowFeature(Window.FEATURE_LEFT_ICON); setContentView(R.layout.activity_main); getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.ic_launcher); } private void showFEATURE_NO_TITLE() { requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); // 加上这句设置为全屏 不加则只隐藏title getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); } private void showFEATURE_PROGRESS() { requestWindowFeature(Window.FEATURE_PROGRESS); setProgressBarVisibility(true); setContentView(R.layout.activity_main); setTitle(""); getWindow().setFeatureInt(Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON); // 通过线程来改变ProgressBar的值 new Thread(new Runnable() { public void run() { for (int i = 0; i < 10; i++) { try { Thread.sleep(1000); Message m = new Message(); m.what = (i + 1) * 20; MainActivity.this.myMessageHandler.sendMessage(m); } catch (Exception e) { e.printStackTrace(); } } } }).start(); } Handler myMessageHandler = new Handler() { @Override public void handleMessage(Message msg) { // 设置标题栏中前景的一个进度条进度值 setProgress(100 * msg.what); // 设置标题栏中后面的一个进度条进度值 setSecondaryProgress(100 * msg.what + 10); super.handleMessage(msg); } }; }
做主页面显示的activity_main.xml没有什么特殊处理,默认的就可以。
进度条显示文件procress.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" > <ProgressBar style="@android:style/Widget.ProgressBar.Horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
自定义标题文件customtitle.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="自定义标题" android:textSize="13sp"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="自定义标题" android:textSize="13sp"/> </LinearLayout>
在运行自定义标题时会发现提示错误:You cannot combine custom titles with other title features,这是因为默认情况下已经有了标题,再加自定义的标题
就会出错,因此需要在style.xml文件中修改默认theme,代码如下:
<!-- Application theme. --> <style name="AppTheme" parent="AppBaseTheme"> <!-- All customizations that are NOT specific to a particular API-level can go here. --> <!-- 避免重复设置标题 --> <item name="android:windowActionBar">false</item> <!-- 设置标题栏宽度 --> <item name="android:windowTitleSize">50dp</item> <!-- <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item> --> </style> <!-- 设置标题栏背景 --> <!-- <style name="CustomWindowTitleBackground"> <item name="android:background">#00cc00</item> </style> -->