【问题标题】:is an android shape with a gradient border and transparent middle possible?具有渐变边框和透明中间的 android 形状可能吗?
【发布时间】:2022-01-06 03:24:41
【问题描述】:

我希望通过渐变边框使按钮的中间透明,我不确定如何使用图层列表来完成。我知道same question 已经存在,但答案还不够,因为他们只是在里面添加了另一个渐变。中间必须是透明的,这样才能看到渐变背景。

这是我当前按钮的代码,我的背景为中心。

<layer-list>
    <item >
        <shape android:shape="rectangle" android:innerRadius="8dp">
            <corners android:radius="@dimen/secondary_button_roundness" />
            <gradient
                android:startX="0"
                android:startY="0"
                android:endX="20"
                android:endY="0"
                android:endColor="@color/primaryGradientStart"
                android:startColor="@color/primaryGradientEnd"
                android:angle="180" >
            </gradient>
        </shape>
    </item>
    <item
        android:bottom="1dp"
        android:left="1dp"
        android:right="1dp"
        android:top="1dp">
        <shape android:shape="rectangle" android:innerRadius="8dp">
            <corners android:radius="@dimen/secondary_button_roundness" />
            <solid android:color="@color/Background" />
        </shape>
    </item>
</layer-list>

【问题讨论】:

    标签: android xml android-studio drawable


    【解决方案1】:

    您可以使用 jetpack compose 实现此目的,然后您可以使用互操作性 API

    @Composable
    fun SetRing() {
        Box(
            modifier = Modifier
                .background(Color.Transparent)
                .border(
                    2.dp,
                    Brush.linearGradient(
                        colors = listOf(
                            Color.Yellow,
                            Color.Blue
                        )
                    ),
                    shape = RoundedCornerShape(5.dp)
                )
                .padding(6.dp)
        ) {
            Text(text = "Hiphop")
        }
    }
    

    互操作性 API, 在 XML 文件中

    <androidx.compose.ui.platform.ComposeView
        android:id="@+id/compose_interest"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    

    output

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-02
      相关资源
      最近更新 更多