【问题标题】:How to create custom button view that can accept corner radius and color dynamically and change accordingly如何创建可以动态接受角半径和颜色并相应更改的自定义按钮视图
【发布时间】:2019-07-25 06:47:42
【问题描述】:

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

【问题讨论】:

  • 如果你只想添加圆角半径和背景,为什么不使用CardView
  • 不,我想要按钮,还有我自己的自定义按钮,这样我就可以在任何地方使用它,这样我就不必为每个新按钮创建形状文件

标签: java android xml android-custom-view


【解决方案1】:

您可以使用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'

【讨论】:

    【解决方案2】:

    1.在你的drawable文件夹中创建一个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" />
    

    【讨论】:

    • 我说我不想每次创建按钮时都使用形状,我想消除这种依赖关系。我想创建一个自定义视图,这样我就可以每次都使用它,而不是为每个按钮创建形状 xml。
    【解决方案3】:

    请试试这个

     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
    

    【讨论】:

    • 有用吗?如果您有任何疑问,请回复?
    【解决方案4】:

    您可以使用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));
    

    我希望它对你有用。

    【讨论】:

      【解决方案5】:
          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);
      

      添加 drwaable 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);
      

      【讨论】:

        猜你喜欢
        • 2018-07-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-14
        相关资源
        最近更新 更多