带圆角和带颜色边框的按钮

Button with rounded corners and border with color

我需要你的帮助来解决我正在尝试做的事情,我一直在尝试制作一个带圆角的按钮并只显示它的边框,我需要能够根据我的需要以编程方式更改颜色从 Web 服务获取,到目前为止,我尝试添加带有可绘制对象的形状,它给出了带有边框颜色的圆形,但我不知道是否可以更改它的颜色,因为它默认添加在可绘制对象中

<?xml version="1.0" encoding="UTF-8"?>

<stroke android:width="3dp"
    android:color="#ff000000"
    />

<padding android:left="1dp"
    android:top="1dp"
    android:right="1dp"
    android:bottom="1dp"
    />

<corners android:bottomRightRadius="7dp"
    android:bottomLeftRadius="7dp"
    android:topLeftRadius="7dp"
    android:topRightRadius="7dp"/>

那是我使用的可绘制对象,然后我尝试添加形状,为按钮创建自定义 class 并更改 onDraw 方法,我得到了一个形状,但有点奇怪

@Override
protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub

    Paint paint = new Paint();
    paint.setStyle(Paint.Style.STROKE);
    paint.setColor(strokeColor);
    paint.setStrokeWidth(5.0f);

    int  h = this.getHeight();
    int  w = this.getWidth();
    //final RectF rect = new RectF();

    RectF oval1 = new RectF(0, 0, w, h);
    canvas.drawRoundRect(oval1, 40, 40, paint);

}

并且出于某种原因,除了奇怪的形状之外,我使用设置文本方法以编程方式添加文本并且它没有显示,它获得笔划的颜色而不是文本

buttonCTA = ButterKnife.findById(this, R.id.btnCTA);
        buttonCTA.setTextColor(Color.parseColor(valueColor));
        buttonCTA.setStrokeColor(valueColor);
        buttonCTA.setText("test");

这就是您所需要的。

    public static void setRoundedDrawable(Context context, View view, int backgroundColor, int borderColor) {
    GradientDrawable shape = new GradientDrawable();
    shape.setShape(GradientDrawable.RECTANGLE);
    shape.setCornerRadius(20f);
    shape.setColor(backgroundColor);
    if (borderColor != 0){
        shape.setStroke(2f, borderColor);
    }
    view.setBackgroundDrawable(shape);
}

您可以根据需要更改角半径和描边宽度。 希望对您有所帮助!

使用GradientDrawable获取视图和修改:

GradientDrawable drawable = (GradientDrawable)buttonCTA.getBackground();
drawable.setStroke(3, Color.your_color); 

这里 3 是预定义的笔划宽度,Color.your_color 是颜色,它将来自 WebService

在可绘制文件夹中创建 round_background.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <stroke
        android:width="3dp"
        android:color="#ff000000" />

    <padding
        android:bottom="1dp"
        android:left="1dp"
        android:right="1dp"
        android:top="1dp" />

    <corners
        android:bottomLeftRadius="7dp"
        android:bottomRightRadius="7dp"
        android:topLeftRadius="7dp"
        android:topRightRadius="7dp" />
</shape>

设为背景

 <Button
        android:id="@+id/mybutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/round_background"
        android:text="Hello World!" />

在运行时使用任何视图更改它。

 Button button = (Button) findViewById(R.id.mybutton);
 GradientDrawable drawable = (GradientDrawable)button.getBackground();
 drawable.setStroke(2, Color.YELLOW);

实际上,您可以使用 Material 设计按钮轻松完成此操作。恕我直言,使用 material 设计是 标准方式 在 Android.

中设置样式

第 1 步:

将以下依赖项添加到模块的 build.gradle 文件并同步项目。

implementation 'com.google.android.material:material:1.0.0'

第 2 步:

将您的 style.xml 应用主题更改为继承 here 中提到的任何 material 个组件主题。

例如:

<style name="AppTheme" parent="Theme.MaterialComponents.NoActionBar">
...
</style>

第 3 步:

将按钮的 xml 更改为如下所示

<com.google.android.material.button.MaterialButton
            android:text="@string/button_text"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:id="@+id/btnRound"
            style="@style/Widget.MaterialComponents.Button.OutlinedButton"
            app:cornerRadius="1000dp"
            app:strokeColor="@color/colorPrimaryDark"/>
  • 您可以使用 app:cornerRadius 属性值更改按钮的圆度。
  • 将按钮的 style 属性设置为 @style/Widget.MaterialComponents.Button.OutlinedButton 以使按钮轮廓化。
  • 使用app:strokeColor属性值设置轮廓颜色。

参考文献: