定义一个适配沉浸式状态栏 的 TitleBar

第一步,定义一个TitleBar

我使用自定义 FrameLayout 后inflate一个布局的方式来静态的使用一个写好的布局,免去手写布局的麻烦

public class TitleBar extends FrameLayout {
LayoutInflater.from(context).inflate(R.layout.myview_title_bar, this);//直接改变布局,从而静态的使用子控件\

第二步,使用沉浸状态栏后测试效果

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#F00"
    android:orientation="vertical">

    <com.dju.sc.x.view.TitleBar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/blue"
        android:fitsSystemWindows="true"
        app:title_text="标题栏" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:background="#FF0"
        android:text="主体内容\n第二行\n第三行\n第四行" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

测试页面是这个样子:

顶部标题栏TextView、输入框、背景

看到标题栏是白色的,是因为标题栏是蓝色的FrameLayout中嵌套的TitleBar布局是白色的。

实际运行效果:

发现顶部状态栏变成了蓝色,看来这是自定义TitleBar的问题,fitsSystemWindows属性让TitleBar的paddingTop增加了一个状态栏高度,但由于TitleBar的子View高度固定了,导致TitleBar本身的蓝色漏出来。若要避免这种情况可以1、将TitleBar子View的高度固定为撑满,而TitleBar高度设为固定,2、将TitleBar的背景色设为同子View的颜色。3、改为给TItleBar子View设置fitsSystemWindows属性。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值