Android中的圆形按钮

Arn*_* C. 69 android rounded-corners android-button

我想在Android程序中创建圆形按钮.我看过 如何用圆角创建EditText?

我想要实现的是:

  1. 圆形边缘按钮
  2. 更改不同状态下的按钮背景/外观(如Onclick,Focus)
  3. 使用我自己的PNG作为背景而不是创建形状.

CSm*_*ith 119

您可以执行圆角按钮而无需使用ImageView.

后台选择器资源,button_background.xml:

<?xml version="1.0" encoding="utf-8" ?> 
     <selector xmlns:android="http://schemas.android.com/apk/res/android">
     <!--  Non focused states 
      --> 
      <item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/button_unfocused" /> 
      <item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/button_unfocused" /> 
     <!--  Focused states 
      --> 
      <item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/button_focus" /> 
      <item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/button_focus" /> 
     <!--  Pressed 
      --> 
      <item android:state_pressed="true" android:drawable="@drawable/button_press" /> 
    </selector>
Run Code Online (Sandbox Code Playgroud)

对于每个州,都是一个可绘制的资源,例如button_press.xml:

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
  <stroke android:width="1dp" android:color="#FF404040" /> 
  <corners android:radius="6dp" /> 
  <gradient android:startColor="#FF6800" android:centerColor="#FF8000" android:endColor="#FF9700" android:angle="90" /> 
</shape>
Run Code Online (Sandbox Code Playgroud)

注意corners属性,这会让你圆角!

然后在按钮上设置背景drawable:

android:background="@drawable/button_background"
Run Code Online (Sandbox Code Playgroud)

编辑(9/2018):可以使用相同的技术创建圆形按钮.圆形实际上只是一个方形按钮,半径大小设置为方形边的1/2

此外,在上面的示例中,stroke并且gradient不是必需的元素,它们只是示例和方式,您将能够看到圆角形状

  • @ ikartik90,使用drawable.通常,其他文件夹用于覆盖"基础"可绘制文件夹中的密度特定资产或设置的设置,在此示例中,不存在任何密度特定覆盖. (3认同)
  • 我是android开发的新手,这个问题可能真是天真:我应该将XML文件添加到哪个“ drawable”文件夹中?我有四个不同的“ drawable”文件夹:“ drawable-hdpi”;`drawable-mdpi`; `drawable-xhdpi`; `drawable-xxhdpi`。 (2认同)

Pir*_*hah 69

如果您需要Android中的圆角按钮,则创建一个XML文件"RoundShapeBtn.xml"作为drawable.

 <?xml version="1.0" encoding="utf-8"?>
  <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" android:padding="10dp">   
  <solid android:color="#6E6E6E"/> <!-- this one is ths color of the Rounded Button -->
  <corners
   android:bottomRightRadius="10dp"
   android:bottomLeftRadius="10dp"
   android:topLeftRadius="10dp"
   android:topRightRadius="10dp"/>
</shape>
Run Code Online (Sandbox Code Playgroud)

将其添加到您的按钮代码:

android:background="@drawable/RoundShapeBtn"
Run Code Online (Sandbox Code Playgroud)

  • 实际上,如果使用CapitalLettersInTheName命名文件,则会出现构建错误.. :) (3认同)

Aru*_*dra 14

在android中的drawable文件夹中创建xml文件,如:

rounded_button.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">

    <corners android:radius="20dp"/> // if you want clear round shape then make radius size is half of your  button`s height.
    <solid android:color="#EEFFFFFF"/> // Button Colour
    <padding
        android:bottom="5dp"
        android:left="10dp"
        android:right="10dp"
        android:top="5dp"/>

</shape>
Run Code Online (Sandbox Code Playgroud)

现在这个xml文件作为你的按钮背景.

 <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@drawable/rounded_button"
        android:text="@string/button_text"
        android:textColor="@color/black"/>
Run Code Online (Sandbox Code Playgroud)


Chr*_*rry 8

Google建议您不要模仿其他平台的UI元素.我不会在Android应用程序中放置圆形iOS样式按钮.

  • 开发人员决定将丑陋的用户界面作为标准,这不是我们的错. (54认同)
  • @lase 完全不同意这些指导方针只是这样,它们不是硬性规则 (2认同)

Cod*_*lan 5

ImageView像这样扩展:

public class RoundedImageView extends ImageView {
  private static final String TAG = "RoundedImageView";

  private float mRadius = 0f;

  public RoundedImageView(Context context) {
    super(context);
  }

  public RoundedImageView(Context context, AttributeSet attrs) {
    super(context, attrs);

    // retrieve styles attributes
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RoundedView);
    mRadius = a.getDimension(R.styleable.RoundedView_radius, 0f);
    a.recycle();
  }

  @Override
  protected void onDraw(Canvas canvas) {
    // only do this if we actually have a radius
    if(mRadius > 0) {
      RectF rect = new RectF(0, 0, getWidth(), getHeight());
      Path clipPath = new Path();
      clipPath.addRoundRect(rect, mRadius, mRadius, Path.Direction.CW);
      canvas.clipPath(clipPath);
    }
    super.onDraw(canvas);
  }
}
Run Code Online (Sandbox Code Playgroud)

并对其应用常规的背景资源,并且应将其剪裁成圆角。