android开发沉浸式标题栏_Android沉浸式(侵入式)标题栏(状态栏)Status(一)-阿里云开发者社区...



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

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

三步:

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

package="zhangphil.myapplication">

android:allowBackup="true"

android:icon="@mipmap/ic_launcher"

android:label="@string/app_name"

android:supportsRtl="true"

android:theme="@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相同:

true

true

(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代码:

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="@android:color/holo_orange_dark"

android:orientation="vertical">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center"

android:background="@android:color/white"

android:text="zhang phil @ csdn" />

代码运行结果:

TextView跑到顶部状态栏下面去了,这显然不合适,在activity_main.xml代码中增加android:fitsSystemWindows="true" :

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="@android:color/holo_orange_dark"

android:fitsSystemWindows="true"

android:orientation="vertical">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center"

android:background="@android:color/white"

android:text="zhang phil @ csdn" />

代码运行结果:

显示就正常了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值