如何创建可以动态接受角半径和颜色并相应更改的自定义按钮视图

How to create custom button view that can accept corner radius and color dynamically and change accordingly

如何创建可以动态接受圆角半径和背景颜色的自定义按钮视图,而不是为每个按钮制作形状文件。 我知道我可以扩展到按钮 class 并创建属性集来接受这些值,但只是不知道如何更改按钮的角半径。

1.Create 在您的可绘制文件夹中创建一个 xml 文件,例如 mybutton.xml 并粘贴以下标记:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true" >
        <shape android:shape="rectangle"  >
            <corners android:radius="3dip" />
            <stroke android:width="1dip" android:color="#5e7974" />
            <gradient android:angle="-90" android:startColor="#345953" android:endColor="#689a92"  />            
        </shape>
    </item>
    <item android:state_focused="true">
        <shape android:shape="rectangle"  >
            <corners android:radius="3dip" />
            <stroke android:width="1dip" android:color="#5e7974" />
            <solid android:color="#58857e"/>       
        </shape>
    </item>  
    <item >
       <shape android:shape="rectangle"  >
            <corners android:radius="3dip" />
            <stroke android:width="1dip" android:color="#5e7974" />
            <gradient android:angle="-90" android:startColor="#8dbab3" android:endColor="#58857e" />            
       </shape>
    </item>
</selector>

下面是按钮代码

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:textColor="#ffffff"
    android:background="@drawable/mybutton"
    android:text="Buttons" />

您可以使用MaterialButton

<com.google.android.material.button.MaterialButton
    android:id="@+id/btnWithdraw"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="30dp"
    android:gravity="center"
    android:padding="15dp"
    android:text="@string/withdraw"
    android:textAllCaps="false"
    android:textColor="@android:color/white"
    android:textSize="15sp"
    android:textStyle="bold"
    app:backgroundTint="@color/colorLightRed"
    app:cornerRadius="10dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/edtAgentPin" />

要更改背景颜色,请使用此

btnWithdraw.backgroundTintList = ColorStateList.valueOf(Color.BLUE)

要更改 cornerRadius 使用这个

rootView.btnWithdraw.cornerRadius = 20

注意: 确保您在 Build.Gradle 文件

中添加了以下 dependencies
implementation 'com.google.android.material:material:1.0.0'

请试试这个

 val shape = GradientDrawable()
shape.cornerRadius = 18f

// add some color
// You can add your random color generator here
// and set color
shape.setColor(Color.RED);
// now find your view and add background to it
view.btn.background = shape

您可以使用 CardView 动态创建具有圆角半径和颜色的自定义按钮。

XML代码:

<android.support.v7.widget.CardView
        android:id="@+id/main_card"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:cardCornerRadius="20dp"
        app:cardElevation="5dp"
        app:cardUseCompatPadding="true"
        app:cardBackgroundColor="@color/orange"
        android:layout_centerInParent="true">
        <LinearLayout
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:gravity="center">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Text"
                android:textColor="#000"/>
        </LinearLayout>
    </android.support.v7.widget.CardView>

动态更改颜色:

CardView main_card=findViewById(R.id.main_card);
main_card.setCardBackgroundColor(getResources().getColor(R.color.colorAccent));

希望对你有用。

    Button btn = new Button(this);
    btn.setText("Submit");
    final int sdk = android.os.Build.VERSION.SDK_INT;
    if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
      btn.setBackgroundDrawable(ContextCompat.getDrawable(context,R.drawable.ready) );
    } else {
      btn.setBackground(ContextCompat.getDrawable(context, R.drawable.ready));
    }
    LinearLayout linearLayout = (LinearLayout)findViewById(R.id.buttonlayout);
    LayoutParams buttonlayout = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    linearLayout.addView(btn, buttonlayout);

添加可绘制对象R.drawable.library_cirecle

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/outerRectangle">
    <shape android:shape="oval" >
        <solid android:color="#FCD366" />

        <stroke
            android:width="1dp"
            android:color="@android:color/darker_gray" />
    </shape>
</item>

在代码中更改颜色

Drawable tempDrawable = getResources().getDrawable(R.drawable.library_cirecle);
LayerDrawable bubble = (LayerDrawable) tempDrawable; (cast to root element in xml)
GradientDrawable solidColor = (GradientDrawable) bubble.findDrawableByLayerId(R.id.outerRectangle);
solidColor.setColor(colorToPaint);
imageView.setImageDrawable(tempDrawable);