获取圆角的几种方案如下:
方案一:
通过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);
} @Override
protected 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);
}
} @Override
protected 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.RoundedRectLinearLayout
android: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() {
@Override
public 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);
}

为ScrollView增加圆角的三种方式,及自定义属性【在Linearlayout中新增ScrollView支持滚动 后续】的更多相关文章

  1. 三种方式实现观察者模式 及 Spring中的事件编程模型

    观察者模式可以说是众多设计模式中,最容易理解的设计模式之一了,观察者模式在Spring中也随处可见,面试的时候,面试官可能会问,嘿,你既然读过Spring源码,那你说说Spring中运用的设计模式吧, ...

  2. iOS设置圆角的三种方式

    第一种方法:通过设置layer的属性 最简单的一种,但是很影响性能,一般在正常的开发中使用很少. ? 1 2 3 4 5 6 7 UIImageView *imageView = [[UIImageV ...

  3. Android设置ScrollView回到顶部的三种方式 (转)

    一.ScrollView.scrollTo(0,0)  直接置顶,瞬间回到顶部,没有滚动过程,其中Y值可以设置为大于0的值,使Scrollview停在指定位置; 二.ScrollView.fullSc ...

  4. 在Linearlayout中新增ScrollView支持滚动

    https://blog.csdn.net/wenzhi20102321/article/details/53491176 1.一般只需要在布局中加个ScrollView即可 2.如果布局中包含lis ...

  5. JavaScript 基础——使用js的三种方式,js中的变量,js中的输出语句,js中的运算符;js中的分支结构

    JavaScript 1.是什么:基于浏览器 基于(面向)对象 事件驱动 脚本语言 2.作用:表单验证,减轻服务器压力 添加野面动画效果 动态更改页面内容 Ajax网络请求 () 3.组成部分:ECM ...

  6. spring配置datasource三种方式

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp34 spring配置datasource三种方式 1.使用org.spri ...

  7. spring配置datasource三种方式及具体信息

    1.使用org.springframework.jdbc.datasource.DriverManagerDataSource说明:DriverManagerDataSource建立连接是只要有连接就 ...

  8. Spring配置dataSource的三种方式 数据库连接池

    1.使用org.springframework.jdbc.dataSource.DriverManagerDataSource 说明:DriverManagerDataSource建立连接是只要有连接 ...

  9. NGUI注册事件的三种方式

    1.第一种方式 当一个元素要执行某个方法,而这个方法在此元素赋予的脚本上有,那么直接会调用此方法,但此方法的名称必须是内置的固定名称,例如OnClick,OnMouseOver,OnMouseOut等 ...

随机推荐

  1. android悬浮窗口

    悬浮窗原理 做过悬浮窗功能的人都知道, 要想显示悬浮窗, 要有一个服务运行在后台, 通过getSystemService(Context.WINDOW_SERVICE)拿到WindowManager, ...

  2. 使用docker搭建最新版本的gitea,并配置HTTPS访问

    使用docker搭建最新版本的gitea,并配置HTTPS访问 博客说明 文章所涉及的资料来自互联网整理和个人总结,意在于个人学习和经验汇总,如有什么地方侵权,请联系本人删除,谢谢! 简介 之前有搭建 ...

  3. 01 Linux系统配置初始化

    #/bin/bash export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin # 更改主机名 # hostnam ...

  4. 9.4、安装zabbix(2)

    8.从节点安装: (1)安装zabbix-agent: 1)下载zabbix-agent并安装: mkdir -p /tools/ cd /tools/ wget https://mirrors.tu ...

  5. 16、mysql主从复制问题总结

    16.1.主库"show master status"没有结果: 1.原因: 主库binlog功能开关没有改或没有生效: 2.解决办法: (1)[root@backup ~]#eg ...

  6. hud 3308 LCIS 线段树 区间合并

    题意: Q a b 查询[a, b]区间的最长连续递增子序列的长度 U a b 将下表为a的元素更新为b 区间合并一般都有3个数组:区间最值,左区间最值和右区间最值 具体详见代码 #include & ...

  7. Java程序设计(2021春)——第一章课后题(选择题+编程题)答案与详解

    Java程序设计(2021春)--第一章课后题(选择题+编程题)答案与详解 目录 Java程序设计(2021春)--第一章课后题(选择题+编程题)答案与详解 第一章选择题 1.1 Java与面向对象程 ...

  8. 42. Trapping Rain Water [dp][stack]

    description: Given n non-negative integers representing an elevation map where the width of each bar ...

  9. java基础---类和对象(1)

    一. 类和对象 面向对象:以属性和行为的观点去分析现实生活中的事物,将功能封装进对象, 强调具备了功能的对象,以类/对象为最小单位,考虑谁来做 面向过程:强调的是功能行为,以函数为最小单位,考虑怎么做 ...

  10. scrapy设置自己的headers referer字段

    1.在middlewares中添加自己的新类: class Mylei(object): def process_request(self,request,spider): referer=reque ...