clipChildren属性

本文介绍如何通过设置clipChildren属性配合layout_gravity属性实现特定的布局效果,如不同高度按钮的对齐,提供了一个简单的示例来展示如何仅用一个属性实现复杂的视觉效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

clipChildren属性表示是否限制子控件在该容器所在的范围内,clipChildren属性配合layout_gravity属性,可以用来设置多余部分的显示位置,我这里举一个简单的例子,比如喜马拉雅FM这个应用的首页:

大家注意看这个应用底部导航栏中中间一个是要比另外四个高的,这种效果很多人就会想到使用一个RelativeLayout布局来实现,其实不用那么麻烦,这种效果一个clipChildren属性就能实现,示例Demo如下:

代码:

<?xml version="1.0" encoding="utf-8"?>  
<RelativeLayout  
    xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:clipChildren="false"  
    tools:context="org.lenve.clipchildren.MainActivity">  
  
    <LinearLayout  
        android:layout_width="match_parent"  
        android:layout_height="48dp"  
        android:layout_alignParentBottom="true"  
        android:background="#03b9fc"  
        android:orientation="horizontal">  
  
        <ImageView  
            android:layout_width="0dp"  
            android:layout_height="match_parent"  
            android:layout_weight="1"  
            android:src="@mipmap/ic_launcher"/>  
  
        <ImageView  
            android:layout_width="0dp"  
            android:layout_height="match_parent"  
            android:layout_weight="1"  
            android:src="@mipmap/ic_launcher"/>  
  
        <ImageView  
            android:layout_width="0dp"  
            android:layout_height="72dp"  
            android:layout_gravity="bottom"  
            android:layout_weight="1"  
            android:src="@mipmap/ic_launcher"/>  
  
        <ImageView  
            android:layout_width="0dp"  
            android:layout_height="match_parent"  
            android:layout_weight="1"  
            android:src="@mipmap/ic_launcher"/>  
  
        <ImageView  
            android:layout_width="0dp"  
            android:layout_height="match_parent"  
            android:layout_weight="1"  
            android:src="@mipmap/ic_launcher"/>  
    </LinearLayout>  
</RelativeLayout> 

大家看只需要在根节点添加clipChildren属性,然后在第三个ImageView上添加layout_gravity属性即可,layout_gravity属性值为bottom表示控件大小超出后控件底部对齐。效果如下:

 

转载于:https://www.cnblogs.com/foxy/p/7849830.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值