【问题标题】:Android Constraint Layout using Flow with elevation and cornor radius使用具有高程和角半径的流的 Android 约束布局
【发布时间】:2020-01-20 15:44:54
【问题描述】:

是否可以在不使用 Cardview 的情况下创建具有高程、角半径等的约束布局? 约束布局有一个“constraintlayout.helper.widget.Flow”,可以很好地打包小部件,但不确定是否可以让它看起来类似于 Cardview

【问题讨论】:

  • 为什么不在 CardView 中使用 ConstraintLayout。
  • 是的,我可以这样做,但是“constraintlayout.helper.widget.Flow”更像是线性布局。我发现用 4 个流元素管理 xml 比管理 4 个卡片视图更容易,每个卡片视图都有一个约束布局。

标签: android android-layout android-constraintlayout android-cardview android-elevation


【解决方案1】:

Flow 类继承自 View 类,因此您也可以在 Flow 中使用高程。

没有类似于 CardView 的cornerRadius 属性。但是您可以创建一个圆角矩形作为可绘制形状并将其设置为流的背景。

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/light_image_background"/>
    <corners android:radius="10dp"/>

</shape>

并将它们设置为像这样流动:

android:background="@drawable/rounded_image_background"
android:elevation="6dp"

在保持扁平层次结构的同时,您将拥有圆形背景。

其他可能的解决方案是从 Flow 类扩展并覆盖 onDraw 以在背景中绘制圆角矩形。应该是这样的:

class RoundedFlow @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : Flow(context, attrs, defStyleAttr) {
    
    private val backgroundRect : RectF = RectF(0f, 0f, 200f, 200f) 
    private val backgroundCornerRadius = 20f
    private val backgroundPaint : Paint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        color = Color.YELLOW
    }

    override fun onDraw(canvas: Canvas?) {
        backgroundRect.set(0f, 0f, measuredWidth.toFloat(), measuredHeight.toFloat())
        canvas?.drawRoundRect(backgroundRect, backgroundCornerRadius , backgroundCornerRadius, backgroundPaint)
    }
}

【讨论】:

    猜你喜欢
    • 2022-01-07
    • 2018-04-17
    • 2018-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-02
    • 1970-01-01
    相关资源
    最近更新 更多