【问题标题】:How I can create rounded layout with two vertical line?如何用两条垂直线创建圆形布局?
【发布时间】:2019-01-03 13:43:23
【问题描述】:

我想在圆形布局中放置两条垂直线(线性或相对)

喜欢这张照片:

编辑:

我的尝试:

<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>

结果:

【问题讨论】:

  • 从圆角开始。这已经谈了很多次了。然后在生产线上工作。提示:这一切都将在 XML 中完成。
  • @DroiDev 请帮我举个例子。
  • @Zardchoobe 请停止偷懒。这里有很多关于 android 视图的圆角的例子。
  • @DroiDev 我尝试了 3 天。我想要圆角布局中的圆角线!这是一项艰巨的任务。
  • 想要更简单的方法吗?考虑使用 9patch png。可能会容易得多。不要对我大喊大叫。 ;)

标签: android android-layout


【解决方案1】:

剪辑是一项昂贵的操作,并且只有 Lollipop 和更高版本才真正支持。

如果您只接受 Lollipop+,并且真的想使用 RelativeLayout,您可以致电 myView.setClipToOutline(true)。如果您的 myView 具有圆形背景,这将告诉系统将所有子项裁剪为圆形。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    myView.setClipToOutline(true);
}

如果您需要在 Lollipop 之前的版本上进行圆角剪裁,那么最好的办法是使用叠加层来遮盖您不想看到的视图部分。也就是说,创建一个具有不透明角和一个透明中心的drawable,然后将其应用到顶部您要剪辑的视图上。这实际上不会剪辑您的视图,但会提供这样做的错觉。

在此处了解有关此策略的更多信息:https://www.techrepublic.com/article/pro-tip-round-corners-on-an-android-imageview-with-this-hack/

【讨论】:

    【解决方案2】:

    如果您需要圆角背景之外的透明区域,则不能选择遮盖垂直线的多余部分。在这种情况下,您可以使用自定义 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);
        }
    }
    

    Lollipop(模拟器)的屏幕截图...

    ...和 ​​Jellybean 4.2.2(设备):

    【讨论】:

    • CardView Clipping 在 android 4.3 上不起作用。只有5及以上就可以了。我只想要线性或相对布局。我有疑问地说。
    • @Zardchoobe - 花了我一些时间,但我认为即使在旧设备上也应该可以实现 - 新方法使用自定义视图并适用于 Android 4.2.2
    猜你喜欢
    • 2020-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-03
    • 2019-03-03
    • 1970-01-01
    相关资源
    最近更新 更多