我想在圆角布局中放置两条垂直线(线性或相对)
喜欢这张照片:
编辑:
我的尝试:
<RelativeLayout
android:background="@drawable/rounded_bg"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:layout_width="5dp"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:background="@color/colorPrimaryDark" />
</RelativeLayout>
结果:
答案 0 :(得分:0)
如果需要在圆角背景之外的透明区域,则不能遮盖垂直线的多余部分。在这种情况下,您可以使用自定义View
,它将适用于API级别17 +:
public class RoundedCornersLinearLayout extends LinearLayout{
private Paint backgroundPaint;
private Paint linePaint;
private float cornerRadius;
private float line_width;
private float line_margin;
private RectF rect = new RectF(0, 0, 0,0);
private Path rectPath = new Path();
private Path linePath = new Path();
public RoundedCornersLinearLayout(Context context) {
super(context);
init();
}
public RoundedCornersLinearLayout(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
public RoundedCornersLinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
setWillNotDraw(false);
// add this so Canvas.clipPath() will give the desired result also for devices running Api level 17,
// see https://stackoverflow.com/a/30354461/5015207
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
setLayerType(LAYER_TYPE_SOFTWARE, null);
}
backgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
backgroundPaint.setColor(Color.GREEN);
backgroundPaint.setStyle(Paint.Style.FILL);
linePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
linePaint.setColor(Color.BLUE);
linePaint.setStyle(Paint.Style.FILL);
// with <dimen name="corner_radius">60dp</dimen> in res/values/dimens.xml
cornerRadius = getResources().getDimensionPixelSize(R.dimen.corner_radius);
// <dimen name="line_width">5dp</dimen>
line_width = getResources().getDimensionPixelSize(R.dimen.line_width);
// <dimen name="margin_10dp">10dp</dimen>
line_margin = getResources().getDimensionPixelSize(R.dimen.margin_10dp);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
float measuredWidth = right - left;
float measuredHeight = bottom - top;
rect.set(0, 0, measuredWidth, measuredHeight);
rectPath.reset();
linePath.reset();
rectPath.addRoundRect(rect, cornerRadius, cornerRadius, Path.Direction.CW);
linePath.addRect(measuredWidth - (line_margin + line_width), 0, measuredWidth - line_margin, measuredHeight, Path.Direction.CW);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawRoundRect(rect, cornerRadius, cornerRadius, backgroundPaint);
canvas.clipPath(rectPath);
canvas.drawPath(linePath, linePaint);
}
}
棒棒糖(仿真器)的屏幕截图...
...和Jellybean 4.2.2(设备):
答案 1 :(得分:0)
剪辑是一项昂贵的操作,只有棒棒糖及更高版本才真正支持剪辑。
如果您只对Lollipop +满意,并且确实想使用RelativeLayout
,则可以致电myView.setClipToOutline(true)
。如果您的myView
的背景是圆形的,这将告诉系统将所有子项裁剪为该圆形。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
myView.setClipToOutline(true);
}
如果您需要在Lollipop之前的版本上进行圆角修剪,那么最好的方法是使用覆盖层遮盖您不想看到的部分视图。也就是说,创建一个具有不透明角和透明中心的可绘制对象,并将该应用在要剪切的视图的顶部。这实际上不会削减您的视图,但会提供这样做的错觉。
此处提供有关此策略的更多信息:https://www.techrepublic.com/article/pro-tip-round-corners-on-an-android-imageview-with-this-hack/