view实践妙用方法记录

clipToPadding

查看android中对该属性的注释

Defines whether the ViewGroup will clip its children and resize (but not clip) any
EdgeEffect to its padding, if padding is not zero. This property is set to true by
default.

自己理解这段话为:该属性用于定义ViewGroup是否会根据padding属性重新裁剪和改变子view的大小。
实际测试以后发现,true的时候会Viewgroup会根据padding调整绘制区域,绘制会减去padding的宽度,设置为false的时候,ViewGroup不会调整绘制区域的大小,子view的绘制区域可以包含padding所在的区域。
clipToPadding=true的效果
可以看到view滚动的时候周围是有留白的padding区域的

clipToPadding=false的效果
可以看到滚动的时候padding部分的留白被子view绘制了

当我们使用recyclerview的时候,如果希望padding区域在滚动的时候不可见,我们可以使用clipToPadding=false,达到这个效果

clipChildren

查看android中的注释

Defines whether a child is limited to draw inside of its bounds or not.
This is useful with animations that scale the size of the children to more
than 100% for instance. In such a case, this property should be set to false
to allow the children to draw outside of their bounds. The default value of
this property is true.

该属性的含义为:是否限制view只能在自己的边界内绘制。当在执行缩放动画的时候,view的大小超过100%的时候,可以将这个属性设置为false,这样这个view就可以在超出自己显示区域的边界外显示了。
clipChildren=true的效果
图片没办法超出父布局显示,被裁剪

布局代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/grandfather"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipChildren="true"
    android:orientation="vertical"
    android:gravity="center"
    tools:context=".MainActivity_main">

    <LinearLayout
        android:id="@+id/father"
        android:background="#4CAF50"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:orientation="vertical">

        <ImageView
            android:src="@drawable/kite"
            android:layout_width="300dp"
            android:layout_height="300dp" />
    </LinearLayout>
</LinearLayout>

clipChildren=false的效果

布局代码:
将上面布局代码中根布局中的clipChildren属性设置为false

使用shape实现圆形drawable资源

在实现圆形效果的时候,可以使用shape属性自定义一个drawable来完成:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <corners android:radius="150dp"/>
    <solid android:color="#221F22"/>
</shape>

上面的代码使用shape属性定义了一个半径150dp的圆,当view的宽高都是300dp的时候,得到的效果:

这样我们就实现了一个圆形的背景图,
shape中同样提供了其他api,我们可以实现方形,渐变,环形等视图效果,参见:
https://blog.csdn.net/qq_15128547/article/details/56680494
https://blog.csdn.net/yang5726685/article/details/80740720

将View裁剪成圆形

当我们对ImageView进行裁剪的时候,自己测试了下,需要把图像设置到background属性上,设置到src属性的时候会导致裁剪无效

以recyclerview为例,默认情况下view都是方形的,当需要实现圆形的recyclerview的时候,可以使用OutlineProvider实现一个圆形的RecyclerView:

        mRecyclerView.setOutlineProvider(new ViewOutlineProvider() {
            @Override
            public void getOutline(View view, Outline outline) {
            	// 可以使用setRoundRect()设置形状为圆角矩形阴影轮廓
                outline.setOval(0, 0, view.getWidth(), view.getHeight());
            }
        });
        mRecyclerView.setClipToOutline(true);

给RecyclerView增加上面的代码,我们就可以实现圆形的RecyclerView效果:

Outline接口还提供了setConvexPath(),提供绘制凸多边形的能力,setRoundRect(),提供绘制圆角矩形View的能力。
绘制凸多边形形状

includeFontPadding

textview更改文本大小后文字无法居中,最后发现是顶部的留白跟着变化了,原来textview会在view顶部和底部增加留白,includeFontPadding属性默认是true,设置为false取消顶部留白
android:includeFontPadding=“false”

layout_weight

布局局中如何让view填充界面中剩余的部分,设置layout_weight="1“ ,layout_width=“0dp”

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值