Android沉浸式(侵入式)标题栏(状态栏)Status(一)



Android沉浸式(侵入式)标题栏(状态栏)Status(一)


现在越来越多的APP设计采用这种称之为沉浸式状态栏(Status)的设计,这种沉浸式状态栏又称之“侵入式”状态栏或标题栏,在Android中实现,方案多,也不难。以下以xml方式实现:

三步:
(1) 我的例子中,Androidmanifest.xml文件中定义的app的style为AppTheme:

  1. <?xml version=“1.0” encoding=“utf-8”?>  
  2. <manifest xmlns:android=“http://schemas.android.com/apk/res/android”  
  3.     package=“zhangphil.myapplication”>  
  4.   
  5.     <application  
  6.         android:allowBackup=“true”  
  7.         android:icon=“@mipmap/ic_launcher”  
  8.         android:label=“@string/app_name”  
  9.         android:supportsRtl=“true”  
  10.         android:theme=“@style/AppTheme”>  
  11.         <activity android:name=“.MainActivity”>  
  12.             <intent-filter>  
  13.                 <action android:name=“android.intent.action.MAIN” />  
  14.   
  15.                 <category android:name=“android.intent.category.LAUNCHER” />  
  16.             </intent-filter>  
  17.         </activity>  
  18.     </application>  
  19.   
  20. </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重新修改为:

  1. <resources>  
  2.   
  3.     <style name=“AppTheme” parent=“android:Theme.Holo.Light.NoActionBar”>  
  4.   
  5.     </style>  
  6.   
  7. </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相同:

  1. <resources>  
  2.   
  3.     <style name=“AppTheme” parent=“android:Theme.Holo.NoActionBar.TranslucentDecor”>  
  4.         <item name=“android:windowTranslucentStatus”>true</item>  
  5.         <item name=“android:windowTranslucentNavigation”>true</item>  
  6.     </style>  
  7.   
  8. </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,显示结果达不到本例结果):

  1. package zhangphil.myapplication;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5.   
  6. public class MainActivity extends Activity {  
  7.   
  8.     @Override  
  9.     protected void onCreate(Bundle savedInstanceState) {  
  10.         super.onCreate(savedInstanceState);  
  11.         setContentView(R.layout.activity_main);  
  12.     }  
  13. }  
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代码:

  1. <?xml version=“1.0” encoding=“utf-8”?>  
  2. <LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”  
  3.     android:layout_width=“match_parent”  
  4.     android:layout_height=“match_parent”  
  5.     android:background=“@android:color/holo_orange_dark”  
  6.     android:orientation=“vertical”>  
  7.   
  8.     <TextView  
  9.         android:layout_width=“wrap_content”  
  10.         android:layout_height=“wrap_content”  
  11.         android:layout_gravity=“center”  
  12.         android:background=“@android:color/white”  
  13.         android:text=“zhang phil @ csdn” />  
  14. </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” :

  1. <?xml version=“1.0” encoding=“utf-8”?>  
  2. <LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”  
  3.     android:layout_width=“match_parent”  
  4.     android:layout_height=“match_parent”  
  5.     android:background=“@android:color/holo_orange_dark”  
  6.     android:fitsSystemWindows=“true”  
  7.     android:orientation=“vertical”>  
  8.   
  9.     <TextView  
  10.         android:layout_width=“wrap_content”  
  11.         android:layout_height=“wrap_content”  
  12.         android:layout_gravity=“center”  
  13.         android:background=“@android:color/white”  
  14.         android:text=“zhang phil @ csdn” />  
  15. </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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值