【问题标题】:Setting Stroke on GradientDrawable Android在 GradientDrawable Android 上设置描边
【发布时间】:2021-03-09 20:04:01
【问题描述】:

我正在创建一个自定义按钮,我正在使用 Drawable.setTintList 来设置默认、按下和禁用状态的颜色。
我想实际为按钮添加一个边框,我正在尝试这样做:

val d = newGradientDrawableForShape()
d.setShape(GradientDrawable.RECTANGLE)

d.setColor(Color.WHITE)
d.setStroke(20, Color.GREEN) 

这样,边框是不可见的,但是如果我不使用色调列表,那么我会看到边框。
有没有办法可以使用 setStroke 和 TintList?
我试过 d.setStroke(width, colorList) 也没有用。

【问题讨论】:

    标签: android button gradientdrawable


    【解决方案1】:

    你可以使用带有状态的drawable 示例:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item android:state_pressed="true">
            <shape android:shape="rectangle">
                <corners android:radius="4dp" />
                <stroke android:width="10dp" android:color="#6699ff" />
            </shape>
        </item>
    
        <item>
            <shape android:shape="rectangle">
                <corners android:radius="4dp" />
                <stroke android:width="10dp" android:color="#669900" />
            </shape>
        </item>
    
    </selector>
    

    【讨论】:

    • 嗨,我实际上没有在这里使用任何 xml,因为它将成为库的一部分,因此需要以编程方式进行。另外,我已经用可绘制对象设置了背景。
    【解决方案2】:

    我有同样的问题,但我也实现了另一种方法。

    这是 XML:

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <FrameLayout
            android:id="@+id/view_card_header_large_border_container"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent">
    
            <com.google.android.material.card.MaterialCardView
                android:id="@+id/view_card_header_large_border_base"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="1dp"
                app:cardBackgroundColor="?paper_color"
                app:cardCornerRadius="10dp"
                app:cardElevation="0dp"/>
    
        </FrameLayout>
    
        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/view_card_header_large_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
                ....
        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    代码如下:

        GradientDrawable gradientDrawable = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT,
                new int[] {leftColor, rightColor});
    
        // card background
        gradientDrawable.setAlpha(70);
        gradientDrawable.setCornerRadius(borderBaseView.getRadius());
        container.setBackground(gradientDrawable);
    
        // card border's background, which depends on the margin that is applied to the borderBaseView
        gradientDrawable.setAlpha(90);
        gradientDrawable.setCornerRadius(12dp);
        borderContainer.setBackground(gradientDrawable);
    

    想法是将背景设置为两个视图,顶视图覆盖“边框”基本视图,但留下1dp 边距作为“边框”。

    这样您将在视图中具有渐变背景,并且在边框中也将具有渐变背景。

    【讨论】:

      【解决方案3】:

      你试过了吗:

      setStroke(
           (strokeSize * resources.displayMetrics.density).toInt(), 
           ColorStateList.valueOf(ContextCompat.getColor(context,colorRes)),
           0f,
           0f
      )
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多