You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现中间带水平透明镂空的半透明布局(ShapeDrawable内边距调整无效)

如何实现中间带水平透明镂空的半透明布局(ShapeDrawable内边距调整无效)

我需要做一个半透明的布局,中间有一个完全透明的矩形区域。我找到的最佳方案是某社区的建议,效果还不错,但问题是我需要这个镂空区域是水平方向的(也就是垂直方向的“内边距”比水平方向大)。那个帖子里提到可以通过代码自定义Shape来实现,但我刚接触这个语言,没能成功。目前我已经写了这些代码:

activity.xml 布局代码

<androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:background="@drawable/pattern"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toTopOf="parent">
            <FrameLayout
                android:id="@+id/hole_frame"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent">
            </FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

activity.kt 代码

val frameLayout = findViewById<FrameLayout>(R.id.hole_frame)
val drawable = ShapeDrawable(RectShape())
drawable.paint.color = -0x34000000
drawable.paint.style = Paint.Style.STROKE
drawable.paint.strokeWidth = 200f
frameLayout.background = drawable

当前实现的效果如下:
当前效果

我已经尝试用drawable.setPadding()来调整内边距,但没有效果。如果你有解决这个问题的方法,或者其他实现中间矩形镂空布局的方案,我会非常感激。

备注:内容来源于stack exchange,提问作者Aethia

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.15 09:43:07