为了账号安全,请及时绑定邮箱和手机立即绑定

安卓实现局部界面遮罩效果

标签:
Android

背景

列表展示数据时,有些数据因为失效了,需要灰化展示。但是因为每一行数据里面包含多个view(如包含用户姓名、性别等等信息),不方便对每个view进行灰化设置,所以需要一个遮罩层将整行遮盖来达到灰化效果。大致效果如下:


webp

解决方案

列表的item布局采用RelativeLayout或者ConstraintLayout,在layout中增加一个空的view(遮罩层),刚好能盖住其他view,默认设置为不可见。在adapter里根据数据的有效性设置是否打开这层遮罩层。

带遮罩的item布局文件样例如下:
其中maskLayer为遮罩层view,如下几点需要重点注意

  1. android:layout_height="30dp" 这个高度要设置为固定的值,必须刚好和view原本高度相同,不能设置为wrap_content或match_parent。所以此方案不适用于item的高度不固定的情况。

  2. android:background="#80F9F9F9" 设置遮罩层的颜色,前两位是透明度(00到FF,值越小越透明。)

  3. android:clickable="true"和android:focusable="true" 是为了屏蔽点击事件,让点击失效。

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:textColor="@android:color/black"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="TextView" />

    <View
        android:id="@+id/maskLayer"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:background="#80F9F9F9"
        android:clickable="true"
        android:focusable="true"
        android:visibility="gone"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" /></android.support.constraint.ConstraintLayout>



作者:程序园中猿
链接:https://www.jianshu.com/p/db078107fa0c


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消