android clipChildren与clipToPadding

clipChildren

参考自:http://www.2cto.com/kf/201608/537822.html
实现功能:
1.底部导航栏的突出图标
2.viewpager一屏多个界面显示
这里写图片描述

这里写图片描述

如何实现:

1.底部导航栏的突出图标

注意:需放在根节点 android:clipChildren="false"

<?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:id="@+id/activity_clip_children"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipChildren="false"
    tools:context="com.sign.viewpagerdemo.ClipChildrenActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true"
        android:background="#e7d321"
        android:orientation="horizontal">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:scaleType="fitCenter"
            android:src="@mipmap/ic_launcher" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:scaleType="fitCenter"
            android:src="@mipmap/ic_launcher" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="90dp"
            android:layout_gravity="bottom"
            android:layout_weight="1"
            android:scaleType="fitCenter"
            android:src="@mipmap/ic_launcher" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:scaleType="fitCenter"
            android:src="@mipmap/ic_launcher" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:scaleType="fitCenter"
            android:src="@mipmap/ic_launcher" />
    </LinearLayout>
</RelativeLayout>

2.viewpager一屏多个界面显示

  1. 父布局需要 android:clipChildren="false"
  2. ViewPager宽度 android:layout_width="match_parent"
  3. 设置margin android:layout_marginLeft="80dp" android:layout_marginRight="80dp"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.sign.viewpagerdemo.MainActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:clipChildren="false"
        android:gravity="center">

        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager3"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_marginLeft="80dp"
            android:layout_marginRight="80dp"
            android:clipChildren="false" />
    </LinearLayout>

</LinearLayout>

注意:该做法会有一个bug,就是只能滑动中间的那个View,而如果我们想要点着左边或者右边的View滑动怎么办?

解决办法:将父类的touch事件分发至viewPager,linearLayout是ViewPager控件的父容器

linearLayout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return viewPager.dispatchTouchEvent(event);
            }
        });

clipToPadding

当我们为ListView、ScrollView、GridView设置了paddingTop或paddingBottom的时候,我们发现当滑动到顶部和底部的时候,默认情况下padding/margin在滑动中一直存在,view总是不能滑动到最底部和最顶部,看起来很别扭。
如下图,左右两个recyclerview均设置了paddingtop,左边的未设置clipToPadding属性(默认为true),右边的设置clipToPadding为false
这里写图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值