我正在尝试创建一个自定义视图,以垂直或水平绘制一条线。
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
if (orientation == 0)
canvas.drawLine(this.x, this.y, this.width.toFloat(), this.y, paint)
else {
canvas.drawLine(this.x, this.y, this.x, this.y + this.height, paint)
}
}
上面的代码有效,但是如果我想将视图移到FrameLayout
中的重力“末端”,它就会消失。
<LineView
android:layout_width="1px"
android:layout_height="match_parent"
android:orientation="vertical"
app:strokeColor="@color/red" />
有什么建议吗?
如果需要简单的线条,请以1dp的宽度或高度使用。也许不需要创建自定义视图。如何在自定义视图类中定义x
和y
?粘贴整个课程代码。