为ScrollView增加圆角的三种方式,及自定义属性【在Linearlayout中新增ScrollView支持滚动 后续】...
获取圆角的几种方案如下:
方案一:
通过shape来实现,给scrollView增加背景来实现
方案二:
通过自定义ScrollView,还要自定义属性,在dispatchDraw中不停的裁剪
方案三:
用Android 5.0新增的接口,给ScrollView添加setOutlineProvider监听来实现
【注意】:设置圆角时已经要给scrollview设置padding值,不然圆角没效果
demo:
方案一:
首先在res/drawable文件夹下,新建一个文件,命名为:shape_radius.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" ><corners android:radius="@dimen/dialog_keyboard_setting_round"/><solid android:color="#31e10a"/></shape>
然后给scrollview新增background为上面的文件就行了
eg:https://www.cnblogs.com/MianActivity/p/5867776.html
方案二:
自定义布局:
package com.smartisanos.sara.widget;import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Path; import android.graphics.Path.Direction; import android.graphics.RectF; import android.util.AttributeSet; import android.view.View; import android.widget.LinearLayout; import com.smartisanos.sara.R;public class RoundedRectLinearLayout extends LinearLayout {private Path mClip;private float mRadius;private float mRadiusMarginTop;private float mRadiusMarginLeft;private float mRadiusMargeinRight;private float mRadiusMargeinBottom;public RoundedRectLinearLayout(Context context, AttributeSet attrs) {super(context, attrs);init(attrs);}public RoundedRectLinearLayout(Context context, AttributeSet attrs,int defStyle) {super(context, attrs, defStyle);init(attrs);}private void init(AttributeSet attrs) {if (attrs != null) {TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.RoundedRectListView, 0, 0);mRadius = a.getDimensionPixelSize(R.styleable.RoundedRectListView_radius, 0);mRadiusMarginTop = a.getDimensionPixelSize(R.styleable.RoundedRectListView_radius_marginTop, 0);mRadiusMarginLeft = a.getDimensionPixelSize(R.styleable.RoundedRectListView_radius_marginLeft, 0);mRadiusMargeinRight = a.getDimensionPixelSize(R.styleable.RoundedRectListView_radius_marginRight, 0);mRadiusMargeinBottom = a.getDimensionPixelSize(R.styleable.RoundedRectListView_radius_marginBottom, 0);a.recycle();}setLayerType(View.LAYER_TYPE_SOFTWARE, null);}@Overrideprotected void onSizeChanged(int w, int h, int oldw, int oldh) {super.onSizeChanged(w, h, oldw, oldh);if (mRadius > 0) {mClip = new Path();RectF rectRound = new RectF(mRadiusMarginLeft, mRadiusMarginTop, w- mRadiusMargeinRight, h - mRadiusMargeinBottom);mClip.addRoundRect(rectRound, mRadius, mRadius, Direction.CW);}}@Overrideprotected void dispatchDraw(Canvas canvas) {int saveCount = canvas.save();if (mRadius > 0) {canvas.clipPath(mClip);}super.dispatchDraw(canvas);canvas.restoreToCount(saveCount);} }
自定义属性:
在res/values/attrs.xml中
<?xml version="1.0" encoding="utf-8"?> <resources><declare-styleable name="RoundedRectListView"><attr name="radius" format="dimension" /><attr name="radius_marginTop" format="dimension" /><attr name="radius_marginLeft" format="dimension" /><attr name="radius_marginRight" format="dimension" /><attr name="radius_marginBottom" format="dimension" /></declare-styleable> </resources>
布局中:
把LinearLayout改为com.smartisanos.sara.widget.RoundedRectLinearLayout,同时:
<com.smartisanos.sara.widget.LocalSearchLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/result" android:layout_width="match_parent" android:layout_height="match_parent" >
<com.smartisanos.sara.widget.RoundedRectLinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/search_result_bg"android:orientation="vertical"android:paddingTop="7dp"android:layout_marginLeft="6dp"android:layout_marginRight="6dp"app:radius="12dp"app:radius_marginLeft="@dimen/local_search_rect_margin"app:radius_marginRight="@dimen/local_search_rect_margin"app:radius_marginBottom="20dp" >……</com.smartisanos.sara.widget.RoundedRectLinearLayout>
</com.smartisanos.sara.widget.LocalSearchLayout>
方案三:
mSettingRound = IMEContext.getContext().getResources().getDimensionPixelSize(R.dimen.dialog_keyboard_setting_round);mScrollView = (ScrollView) mRootView.findViewById(R.id.dialog_keyboard_setting_scroll);if (Build.VERSION.SDK_INT >= 21) {mScrollView.setOutlineProvider(new ViewOutlineProvider() {@Overridepublic void getOutline(View view, Outline outline) {if (Build.VERSION.SDK_INT >= 21) {outline.setRoundRect(view.getLeft(), view.getTop(), view.getRight(), view.getBottom(), mSettingRound);}}});mScrollView.setClipToOutline(true);}
转载于:https://www.cnblogs.com/longjunhao/p/9056128.html
为ScrollView增加圆角的三种方式,及自定义属性【在Linearlayout中新增ScrollView支持滚动 后续】...相关推荐
- AUTOCAD——总结CAD画圆角的三种方式
CAD画圆角的三种方式 操作方式 1.输入给对象加圆角命令"FILLET"(快捷键:F),按下空格,再输入"r",空格,输入圆角的半径,然后空格,接着选择第一. ...
- 三种方式实现观察者模式 及 Spring中的事件编程模型
观察者模式可以说是众多设计模式中,最容易理解的设计模式之一了,观察者模式在Spring中也随处可见,面试的时候,面试官可能会问,嘿,你既然读过Spring源码,那你说说Spring中运用的设计模式吧, ...
- iOS设置圆角的三种方式
第一种方法:通过设置layer的属性 最简单的一种,但是很影响性能,一般在正常的开发中使用很少. UIImageView *imageView = [[UIImageView alloc]initWi ...
- vue 函数 路由跳转_vue中通过路由跳转的三种方式
router-view 实现路由内容的地方,引入组件时写到需要引入的地方 需要注意的是,使用vue-router控制路由则必须router-view作为容器. 通过路由跳转的三种方式 1.router ...
- spring配置datasource三种方式
转自:http://blog.csdn.net/yangyz_love/article/details/8199207 1.使用org.springframework.jdbc.datasource. ...
- spring配置datasource三种方式 数据库连接
1.使用org.springframework.jdbc.datasource.DriverManagerDataSource 说明:DriverManagerDataSource建立连接是只要有连 ...
- Struts2访问Servlet的三种方式
Struts2访问Servlet的三种方式 基本介绍 在Struts2中,Action并没有直接和Servlet API耦合.虽然Struts2 中的Action访问Servlet API麻烦一些,但 ...
- Spring学习总结3——配置datasource三种方式
为什么80%的码农都做不了架构师?>>> jdbc.properties文件信息 ##Oracle 11g jdbc.driverClassName=oracle.jdbc.O ...
- android sqlite使用之模糊查询数据库数据的三种方式
android sqlite使用之模糊查询数据库数据的三种方式 android应用开发中常常需要记录一下数据,而在查询的时候如何实现模糊查询呢?很少有文章来做这样的介绍,所以这里简单的介绍下三种sql ...
- mysql datasource property_spring配置datasource三种方式
3.使用org.springframework.jndi.JndiObjectFactoryBean 说明:JndiObjectFactoryBean 能够通过JNDI获取DataSource jav ...
最新文章
- 重磅!「肠道菌群」项目征集,百万科研经费助力细分领域全新突破
- 2021厦门高考成绩查询,2021年厦门高考成绩排名查询,成绩什么时候可以查询
- pyrealsense2 sensor.get_option_description 报错 RuntimeError: object doesn't support option #14
- Electron项目怎样打包成桌面exe应用
- 虚拟化技术知识点初步梳理
- python 生成器推导式
- Python 第三方模块之 beautifulsoup(bs4)- 解析 HTML
- 浏览器复制网页链接到微信,显示却只有网页title解决
- URLRewiter使用
- 运用EL表达式进行复杂比较(在JSTL中调用函数)
- 408考研经验分享(待更
- l36h android os,Sony Xperia Z(L36h)怎么刷魅族Flyme OS
- Latex排版大括号让其左对齐
- shipyard中文版安装教程(附安装脚本)
- pointer-events: none;属性。pointer-events: none;看得见摸不着,点击穿透。
- 本周最新文献速递20210815
- 集赞免费领《新程序员》电子刊!
- 共模电感的原理以及使用情况
- oracle awr 执行计划,AWR(五)-利用AWR生成SQL执行计划(SQLREPORT)并进行优化 | 信春哥,系统稳,闭眼上线不回滚!...
- 统计基础之差异性归一化正态分布