Android沉浸式(侵入式)标题栏(状态栏)Status(一)
2016-09-22 14:35
225 查看
Android沉浸式(侵入式)标题栏(状态栏)Status(一)
现在越来越多的APP设计采用这种称之为沉浸式状态栏(Status)的设计,这种沉浸式状态栏又称之“侵入式”状态栏或标题栏,在Android中实现,方案多,也不难。以下以xml方式实现:
三步:
(1) 我的例子中,Androidmanifest.xml文件中定义的app的style为AppTheme:
上面的Androidmanifest.xml是Android Studio自动生成的,同时Android Studio自动在res/values目录下生成的styles.xml文件中定义了AppTheme,我把这个AppTheme重新修改为:
(2)再为Android v19准备一套styles.xml文件。在res/目录下新建一个名为values-v19目录,在res/values-v21目录下再建一个styles.xml文件,注意名字和AppTheme相同:
(3)写一个简单的MainActivity.java测试,MainActivity.java代码(特别注意!本例的MainActivity继承自Activity而不是AppCompatActivity,如果继承自AppCompatActivity,显示结果达不到本例结果):
MainActivity.java加载的activity_main.xml代码:
代码运行结果:
![](http://img.blog.csdn.net/20160922143417181?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
TextView跑到顶部状态栏下面去了,这显然不合适,在activity_main.xml代码中增加android:fitsSystemWindows="true" :
代码运行结果:
![](http://img.blog.csdn.net/20160922143519077?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
显示就正常了。
附录:
1,《Android StatusBarUtil:设置Android系统下方虚拟键键盘透明度》链接:http://blog.csdn.net/zhangphil/article/details/51768318
现在越来越多的APP设计采用这种称之为沉浸式状态栏(Status)的设计,这种沉浸式状态栏又称之“侵入式”状态栏或标题栏,在Android中实现,方案多,也不难。以下以xml方式实现:
三步:
(1) 我的例子中,Androidmanifest.xml文件中定义的app的style为AppTheme:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="zhangphil.myapplication"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
上面的Androidmanifest.xml是Android Studio自动生成的,同时Android Studio自动在res/values目录下生成的styles.xml文件中定义了AppTheme,我把这个AppTheme重新修改为:
<resources> <style name="AppTheme" parent="android:Theme.Holo.Light.NoActionBar"> </style> </resources>
(2)再为Android v19准备一套styles.xml文件。在res/目录下新建一个名为values-v19目录,在res/values-v21目录下再建一个styles.xml文件,注意名字和AppTheme相同:
<resources> <style name="AppTheme" parent="android:Theme.Holo.NoActionBar.TranslucentDecor"> <item name="android:windowTranslucentStatus">true</item> <item name="android:windowTranslucentNavigation">true</item> </style> </resources>
(3)写一个简单的MainActivity.java测试,MainActivity.java代码(特别注意!本例的MainActivity继承自Activity而不是AppCompatActivity,如果继承自AppCompatActivity,显示结果达不到本例结果):
package zhangphil.myapplication; import android.app.Activity; import android.os.Bundle; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
MainActivity.java加载的activity_main.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:background="@android:color/holo_orange_dark" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:background="@android:color/white" android:text="zhang phil @ csdn" /> </LinearLayout>
代码运行结果:
TextView跑到顶部状态栏下面去了,这显然不合适,在activity_main.xml代码中增加android:fitsSystemWindows="true" :
<?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:background="@android:color/holo_orange_dark" android:fitsSystemWindows="true" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:background="@android:color/white" android:text="zhang phil @ csdn" /> </LinearLayout>
代码运行结果:
显示就正常了。
附录:
1,《Android StatusBarUtil:设置Android系统下方虚拟键键盘透明度》链接:http://blog.csdn.net/zhangphil/article/details/51768318
相关文章推荐
- Android沉浸式(侵入式)标题栏(状态栏)Status(一)
- Android沉浸式(侵入式)标题栏(状态栏)Status(三)
- Android沉浸式(侵入式)标题栏(状态栏)Status(三)
- Android沉浸式(侵入式)标题栏(状态栏)Status(三)
- Android沉浸式(侵入式)标题栏(状态栏)Status(三)
- Android沉浸式(侵入式)标题栏(状态栏)Status(二)
- Android沉浸式(侵入式)标题栏(状态栏)Status(二)
- Android沉浸式(侵入式)标题栏(状态栏)Status(一)
- Android沉浸式(侵入式)标题栏(状态栏)Status(一)
- Android沉浸式(侵入式)标题栏(状态栏)Status(二)
- Android沉浸式(侵入式)标题栏(状态栏)Status(二)
- android界面之沉浸式全屏等状态栏标题栏全解析
- TitleLayout——一个Android轻松实现通用、标准、支持沉浸式状态栏的标题栏库
- Android 去掉状态栏 去掉标题栏 沉浸式状态栏 状态栏重叠 布局重叠 SystemBarTintManager systembartint-1.0.4
- TitleLayout——一个Android轻松实现通用、标准、支持沉浸式状态栏的标题栏库
- Android仿百度外卖APP首页状态/标题栏效果(沉浸式状态栏)
- Android底部菜单栏、Android沉浸式状态栏(顶部状态栏修改颜色)、自定义标题栏
- android沉浸式状态栏、fitsSystemWindows、标题栏折叠
- Android UI 之自定义标题栏 + 沉浸式状态栏
- android状态栏标题栏的沉浸式模式