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

问题描述 投票:3回答:4

我需要你帮助我尝试做的事情,我一直在尝试制作一个带圆角的按钮,只显示它的边框,我需要能够以编程方式更改颜色,具体取决于我从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"/>

这是我正在使用的drawable,然后我尝试添加形状为按钮创建一个自定义类并更改onDraw方法,我得到一个形状但有点奇怪

Rounded shape

@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");
android button rounded-corners
4个回答
8
投票

这是你需要的。

    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);
}

您可以根据需要更改拐角半径和行程宽度。希望能帮助到你!


7
投票

在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);

0
投票

使用GradientDrawable获取视图和修改:

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

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


0
投票

实际上,您可以使用Material Design Button轻松完成此操作。恕我直言,使用材料设计是在Android中设计样式的标准方式。

rounded outlined button

步骤1:

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

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

第2步:

更改您的style.xml应用程序主题,以继承任何提到here的材料组件主题。

例如:

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

参考文献:

© www.soinside.com 2019 - 2024. All rights reserved.