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

7

我需要你帮助我完成一个任务,我一直在尝试制作一个圆角按钮并仅显示其边框,我需要能够根据从Web服务获取的信息在程序中改变它的颜色。到目前为止,我已经尝试使用drawable添加形状,这样就可以得到具有边框颜色的圆角形状,但是我不知道是否可能更改它的颜色,因为它是默认添加到drawable中的。

<?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"/>

这是我使用的可绘制对象,然后我尝试添加形状,为按钮创建一个自定义类并更改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);

}

除了奇怪的形状之外,我还使用“setText”方法动态添加文本,但它没有显示出来,它可以获取描边的颜色,但无法显示文本。

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

13

在drawable文件夹中创建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);

9

实际上,您可以很容易地使用Material Design按钮完成此操作。在我看来,在Android中使用Material Design是样式化事物的标准方法。

rounded outlined button

步骤1:

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

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

步骤2:

将您的style.xml应用程序主题更改为继承从这里提到的任何材料组件主题之一。

例如:

<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属性值来设置轮廓颜色。

参考文献:


9
这就是您所需要的东西。
    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);
}

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


这对我有用,只需注意以下几点:Context不是必需的,并且setStroke中的2f警告应该是int而不是float。我最终采取的方法是: public static void setRoundedDrawable(View view, int backgroundColor, int borderColor) { GradientDrawable shape = new GradientDrawable(); shape.setShape(GradientDrawable.RECTANGLE); shape.setCornerRadius(10f); shape.setColor(backgroundColor); if (borderColor != 0){ shape.setStroke(2, borderColor); } view.setBackground(shape); } - ja928

0
使用GradientDrawable获取视图并进行修改:
GradientDrawable drawable = (GradientDrawable)buttonCTA.getBackground();
drawable.setStroke(3, Color.your_color); 

这里的3是预定义的笔画宽度,Color.your_color是从WebService获取的颜色。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接