在进行android开发时,每个Activity会默认带上一个title bar用以显示程序名,有时为了扩大屏幕的显示区域需要去掉这个titlebar,去掉屏幕上的title bar有3个方法,
第一个方法是在代码去掉title bar
在Activity的onCreate中加入如下代码:
放置于setContentView(R.layout.activity_main);之前皆可。
但是使用这种方法,是在Activity将要显示的时候,仍然会出现title bar,然后再将其去掉,用户体验不好。
以下是对this.requestwindowFeature(window.FEATURE_NO_TITLE)的详细解读:
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:标题栏右侧的图标
二、详解
默认显示状态
id="iframe_0.044124332225969676" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fdl.iteye.com%2Fupload%2Fpicture%2Fpic%2F105856%2Ffa6141f6-aac0-3159-8d11-c0b9ced77093.jpg&maxWidth=1027&origin=http://www.cnblogs.com&iframeId=iframe_0.044124332225969676" frameborder="0" scrolling="no" height="156" style="border-style: none; border-width: initial; width: 1027px;">
1.FEATURE_CUSTOM_TITLE详解
this.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); setContentView(R.layout.main);
id="iframe_0.454926626182077" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fdl.iteye.com%2Fupload%2Fpicture%2Fpic%2F105858%2Feec85e62-641d-32c5-9526-05d2e942aa87.jpg&maxWidth=1027&origin=http://www.cnblogs.com&iframeId=iframe_0.454926626182077" frameborder="0" scrolling="no" height="162" style="border-style: none; border-width: initial; width: 1027px;">
这是因为没有设置Featrue
在上面代码后加:getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);
id="iframe_0.5788932766609451" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fdl.iteye.com%2Fupload%2Fpicture%2Fpic%2F105860%2Fc06cd005-b727-372f-b22a-6a287ae6ac15.jpg&maxWidth=1027&origin=http://www.cnblogs.com&iframeId=iframe_0.5788932766609451" frameborder="0" scrolling="no" height="173" style="border-style: none; border-width: initial; width: 1027px;">
自定义标题完成,它是一个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" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:textColor="#000000" android:text="FEATURE_CUSTOM_TITLE" /> </LinearLayout>
2.FEATURE_INDETERMINATE_PROGRESS详解
可以用来表示一个进程正在运行
this.requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); setContentView(R.layout.main); getWindow().setFeatureInt(Window.FEATURE_INDETERMINATE_PROGRESS, R.layout.progress); setProgressBarIndeterminateVisibility(true);
id="iframe_0.25869446424887266" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fdl.iteye.com%2Fupload%2Fpicture%2Fpic%2F105862%2Fd8a306fe-91f1-377a-93c8-ae1c295053eb.jpg&maxWidth=1027&origin=http://www.cnblogs.com&iframeId=iframe_0.25869446424887266" frameborder="0" scrolling="no" height="201" style="border-style: none; border-width: initial; width: 1027px;">
<?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详解
//111 requestWindowFeature(Window.FEATURE_RIGHT_ICON); setContentView(R.layout.main); getWindow().setFeatureDrawableResource(Window.FEATURE_RIGHT_ICON,R.drawable.ic_launcher); //222 requestWindowFeature(Window.FEATURE_LEFT_ICON); setContentView(R.layout.main); getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,R.drawable.ic_launcher);
id="iframe_0.4378406923613887" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fdl.iteye.com%2Fupload%2Fpicture%2Fpic%2F105870%2F59916459-70e1-302e-9b4c-36e4cb549d3f.jpg&maxWidth=1027&origin=http://www.cnblogs.com&iframeId=iframe_0.4378406923613887" frameborder="0" scrolling="no" height="183" style="border-style: none; border-width: initial; width: 1027px;">
id="iframe_0.6350278981536075" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fdl.iteye.com%2Fupload%2Fpicture%2Fpic%2F105864%2Fea603294-3605-3c02-a968-4884a315decd.jpg&maxWidth=1027&origin=http://www.cnblogs.com&iframeId=iframe_0.6350278981536075" frameborder="0" scrolling="no" height="226" style="border-style: none; border-width: initial; width: 1027px;">
4.FEATURE_NO_TITLE详解
this.requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.main);
id="iframe_0.26587097744523325" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fdl.iteye.com%2Fupload%2Fpicture%2Fpic%2F105866%2F75276a27-52c5-3327-abdb-dfb425c9a2c1.jpg&maxWidth=1027&origin=http://www.cnblogs.com&iframeId=iframe_0.26587097744523325" frameborder="0" scrolling="no" height="175" style="border-style: none; border-width: initial; width: 1027px;">
id="iframe_0.007660302120629403" src="https://www.cnblogs.com/show-blocking-image.aspx?url=http%3A%2F%2Fdl.iteye.com%2Fupload%2Fpicture%2Fpic%2F105868%2F087a5675-c936-32b4-9602-d56d1b15d697.jpg&maxWidth=1027&origin=http://www.cnblogs.com&iframeId=iframe_0.007660302120629403" frameborder="0" scrolling="no" height="168" style="border-style: none; border-width: initial; width: 1027px;">
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
完 美 分 割 线
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
第二种方法是使用style配置文件,步骤如下:
1.在res/values文件夹下创建一个xml文件,名为mainStyle.xml,内容如下:
2.然后在AndroidManifest.xml中需要去掉title bar的activities的节点上加上一个样式属性,代码如下:
android:configChanges
在android中每次屏幕的切换动会重启Activity,所以应该在Activity销毁前保存当前活动的状态,在Activity再次Create的时候载入配置。在activity加上android:configChanges="keyboardHidden|orientation"属性,就不会重启activity.而只是调用onConfigurationChanged(Configuration newConfig).这样就可以在这个方法里调整显示方式.
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.himi" android:versionCode="1" android:versionName="1.0">
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".MainActivity" android:label="@string/app_name"
- android:configChanges="keyboardHidden|orientation">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- <uses-sdk android:minSdkVersion="4" />
- </manifest>
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
完 美 分 割 线
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
第三种方法是直接在AndroidManifest.xml中进行修改,
把需要去掉title bar的activities的节点上加上一个样式属性,代码如下:
也可以在AndroidManifest.xml文件的application节点上修改,对所有的activity都有效,代码如下: