Android沉浸式(侵入式)标题栏(状态栏)Status(一)
来源:互联网 发布:windows策略管理 编辑:程序博客网 时间:2024/05/22 17:50
Android沉浸式(侵入式)标题栏(状态栏)Status(一)
现在越来越多的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>
<?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>
<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>
<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);
- }
- }
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>
<?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>
<?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
转自:http://blog.csdn.net/zhangphil/article/details/52622758
- Android沉浸式(侵入式)标题栏(状态栏)Status(一)
- Android沉浸式(侵入式)标题栏(状态栏)Status(一)
- Android沉浸式(侵入式)标题栏(状态栏)Status(二)
- Android沉浸式(侵入式)标题栏(状态栏)Status(三)
- Android沉浸式(侵入式)标题栏(状态栏)Status(二)
- Android沉浸式(侵入式)标题栏(状态栏)Status(三)
- android状态栏标题栏的沉浸式模式
- android沉浸式状态栏、fitsSystemWindows、标题栏折叠
- Android沉浸式状态栏(一)
- android沉浸式状态栏--解决方案(一)
- Android沉浸式状态栏(一)
- Android底部菜单栏、Android沉浸式状态栏(顶部状态栏修改颜色)、自定义标题栏
- 沉浸式状态栏(一)
- 沉浸式状态栏(一)
- Android 安卓 自定义标题栏+沉浸式状态栏
- Android UI 之自定义标题栏 + 沉浸式状态栏
- android沉浸式标题栏
- Android 沉浸式标题栏
- 数组_数组名_数组地址
- 学以致用-使用Spotfire创建自己的简单知识库查询系统
- 【IPFS + 区块链 系列】 入门篇
- weMosD1--12864 OLED/I2C显示
- Spring Boot 热加载
- Android沉浸式(侵入式)标题栏(状态栏)Status(一)
- 【IPFS + 区块链 系列】 入门篇
- 深度学习之卷积神经网络CNN及tensorflow代码实现示例
- 码农和程序员之间的5个关键差异
- 计组2-3章图表汇总
- Guava EventBus体验
- 049day(构造和析构函数的调用时机,递归例题(四则运算表达式求值)的学习)
- Android沉浸式(侵入式)标题栏(状态栏)Status(二)
- js运算符(是用来返回结果的)