【问题标题】:Rounded corners on material button材质按钮上的圆角
【发布时间】:2026-02-14 21:00:02
【问题描述】:

我正在按照this 等问题中的提示来创建像 Material Design 中建议的按钮样式。

但是,我需要更改拐角半径,但无法通过继承 Widget.AppCompat.Button.Colored 样式并设置半径参数来做到这一点。

我怎样才能拥有相同的风格但圆角?

【问题讨论】:

  • 谷歌有新框架,新技术更好Jetpack Compose
  • @Ucdemir 您的评论没有任何意义。这只是本月当前口味的广告。

标签: android material-design android-support-library android-button android-support-design


【解决方案1】:

更新:

下面 Gabriele Mariotti 的回答现在更好了。

旧答案:

你需要继承这种风格。

添加到你的styles.xml:

 <style name="AppTheme.RoundedCornerMaterialButton" parent="Widget.AppCompat.Button.Colored">
        <item name="android:background">@drawable/rounded_shape</item>
 </style>

添加文件drawable/rounded_shape.xml:

<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"   >

    <solid
        android:color="@color/colorAccent" >
    </solid>

    <corners
        android:radius="11dp"   >
    </corners>

</shape>

最后在你的布局中:

 <Button
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="Test Text"
       style="@style/AppTheme.RoundedCornerMaterialButton"/>

编辑:更新了使用主题颜色而不是硬编码颜色的答案。

【讨论】:

  • 但是,这会消除按钮的涟漪效应。如何在自定义按钮上获得涟漪效果?
  • 使用选择器来管理按钮的所有统计信息,因为使用此方法不会在触摸/悬停/交互/禁用等按钮上播放动画。
【解决方案2】:

另外一种简单的方法是将它包裹在cardView周围,记住将cardView的layout_width和layout_height设置为wrap_content,并且按钮所需的所有边距都应该应用于cardView

<android.support.v7.widget.CardView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:cardCornerRadius="8dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginBottom="40dp"
            app:elevation="10dp">

            <android.support.v7.widget.AppCompatButton
                android:id="@+id/login_twitter"
                android:tag="login_twitter"
                android:layout_width="300dp"
                android:layout_height="60dp"
                android:paddingLeft="10dp"
           android:foreground="?attr/selectableItemBackgroundBorderless"
                android:textColor="@color/blue_grey_ligthen_5"
                android:drawableLeft="@drawable/twitter"
                android:background="@color/twitter"
                android:text="@string/login_with_twitter" />
        </android.support.v7.widget.CardView>

【讨论】:

    【解决方案3】:

    我会告诉你我的确切解决方案。在选择器标签内,您可以放置​​项目(按钮的功能)

    选择器标记的第二项具有相反的行为。您可以添加尽可能多的选择器(按钮行为) 添加这个可绘制的 XML 作为按钮的背景 android:background="@drawable/this xml"

        <?xml version="1.0" encoding="utf-8"?>
    <ripple xmlns:android="http://schemas.android.com/apk/res/android"
        android:color="#ffffff"> <!-- this is the ripple color(first touch color changes into this color) -->
        <item>
            <selector> 
                <item android:state_enabled="true">
                    <shape xmlns:android="http://schemas.android.com/apk/res/android"
                        android:shape="rectangle">
                        <!-- default button color -->
    
                        <solid android:color="@color/colorPrimary"></solid>
    
                        <corners android:radius="151dp"></corners>
    
                    </shape>
                </item>
                <item> //first item was state enabled so this is automatically state disabled
                    <shape xmlns:android="http://schemas.android.com/apk/res/android"
                        android:shape="rectangle">
                        <!-- button disabled color opposite behaviour -->
                        <solid android:color="#e9d204"></solid>
    
                        <corners android:radius="151dp"></corners>
    
                    </shape>
                </item>
            </selector>
        </item>
        <item>
            <selector>
                <item android:state_pressed="true">
                    <shape xmlns:android="http://schemas.android.com/apk/res/android"
                        android:shape="rectangle">
                        <!-- button first touch of your finger color -->
                        <solid android:color="#1989fa"></solid>
    
                        <corners android:radius="151dp"></corners>
    
                    </shape>
                </item>
            </selector>
        </item>
        <item>
            <selector>
                <item android:state_hovered="true">
                    <shape xmlns:android="http://schemas.android.com/apk/res/android"
                        android:shape="rectangle">
                        <!-- hovered with a note pencil -->
                        <solid android:color="#4affffff"></solid>
    
                        <corners android:radius="151dp"></corners>
    
                    </shape>
                </item>
            </selector>
        </item>
    
    </ripple>
    

    【讨论】:

      【解决方案4】:

      使用 Material Components Library:

      the dependency 添加到您的build.gradle

      dependencies { implementation ‘com.google.android.material:material:1.3.0’ }
      

      在这种情况下,您可以在布局文件中使用MaterialButton

      <com.google.android.material.button.MaterialButton
         ....
         style="@style/Widget.MaterialComponents.Button"
         app:cornerRadius=".."
         app:strokeColor="@color/colorPrimary"/>
      

      使用app:cornerRadius属性来改变圆角半径的大小。这将圆角指定尺寸。

      您还可以使用shapeAppearanceOverlay 属性自定义角。

      <style name="MyButton" parent="Widget.MaterialComponents.Button.OutlinedButton">
          <item name="shapeAppearanceOverlay">@style/MyShapeAppearance</item>
      </style>
      <style name="MyShapeAppearance">
          <item name="cornerFamily">rounded</item>
          <item name="cornerFamilyTopRight">cut</item>
          <item name="cornerFamilyBottomRight">cut</item>
          <item name="cornerSizeTopLeft">32dp</item>
          <item name="cornerSizeBottomLeft">32dp</item>
      </style>
      

      官方文档是here 和所有的android 规格here


      使用 Jetpack Compose 1.0.x 使用 shape 参数:

          Button( onClick = { /* Do something! */ },
                  shape = RoundedCornerShape(8.dp)) {
              Text("Button")
          }
      

          Button(modifier = Modifier.padding(16.dp),
                  onClick = { /* Do something! */ },
                  shape = RoundedCornerShape(
                      50,  // topEndPercent
                      0,   // topEndPercent
                      0,   // bottomEndPercent
                      50.  // bottomStartPercent
                  )
          ) {
              Text("Button")
          }
      


      旧支持库:

      使用新的Support Library 28.0.0,设计库现在包含Material Button

      您可以将此按钮添加到我们的布局文件中:

      <android.support.design.button.MaterialButton
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="XXXXX"
          android:textSize="18sp"
          android:backgroundTint="@color/colorPrimary"
          app:icon="@drawable/ic_android_white_24dp" />
      

      您可以使用此属性设置拐角半径

      • app:cornerRadius: 用于定义按钮角的半径

      dependencies {
        implementation 'com.android.support:design:28.0.0'
      }
      

      【讨论】:

      • 这将是最好的解决方案,但在 Android Studio 3.2 发布后,目前 Android Studio 3.1 将无法编译 Android P
      • @amorenew 这并不完全正确。它适用于 android Studio 3.1 或更高版本。是的,它需要使用 Android P 编译
      • 使用新的 MaterialButton 时,要使其正常工作,您还必须使用新的 MaterialComponents 主题。例如,将您的主题设置为从 Theme.MaterialComponents.Light.NoActionBar 而不是 Theme.AppCompat.Light.NoActionBar 扩展
      • 我认为应该是‘com.google.android.material:material:1.1.0-alpha08’或更高,1.0.0中没有shapeAppearance的迹象
      • 将材质库更新到 1.2.0 后转角已经消失
      【解决方案5】:

      带有波纹效果的圆形材质按钮

      在drawable文件夹中创建一个文件ripple.xml

      <?xml version="1.0" encoding="utf-8"?>
      <ripple xmlns:android="http://schemas.android.com/apk/res/android"
          android:color="?android:attr/colorControlHighlight">
          <item android:id="@android:id/mask">
              <shape android:shape="rectangle">
                  <solid android:color="@color/colorPrimary" />
                  <corners android:radius="20dp" />
              </shape>
          </item>
          <item android:drawable="@drawable/rounded_shape" />
      </ripple>
      

      在drawable文件夹中创建一个文件rounded_shape.xml

      <?xml version="1.0" encoding="utf-8"?>
      <shape
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:shape="rectangle"   >
          <solid
              android:color="@color/colorPrimary" >
          </solid>
          <corners
              android:radius="20dp"   >
          </corners>
      </shape>
      

      在你的按钮上:

      <Button
      android:id="@+id/button1"
      android:background="@drawable/ripple"
      android:text="Login"/>
      

      【讨论】:

      • 这不是 Support Library v28 MaterialButton 提供的吗?
      【解决方案6】:

      现在将MaterialButton 用于圆形按钮,您可以用它做更多的事情。请点击链接

      并为圆角添加app:cornerRadius="8dp"

      别忘了在 build.gradle

      中添加 google 材料库
      implementation "com.google.android.material:material:1.1.0"
      

      【讨论】:

      • 如何将其设置到styles.xml 文件中?我尝试设置&lt;item name="cornerRadius"&gt;25dp&lt;/item&gt;,但没有工作。
      • 您使用的是 com.google.android.material 库和材料按钮,请仔细阅读链接
      • 是的,我忘了添加材质按钮
      • 保持简单。非常感谢。
      【解决方案7】:

      试试下面的代码 创建一个名为 circular_button.xml 的可绘制文件并插入下面的代码

      <?xml version="1.0" encoding="utf-8"?>
      <shape xmlns:android="http://schemas.android.com/apk/res/android"
      android:shape="rectangle">
      <solid android:color="#008577" />
      <corners android:bottomRightRadius="100dp"
          android:bottomLeftRadius="100dp"
          android:topRightRadius="100dp"
          android:topLeftRadius="100dp"/>
      </shape>
      

      然后把按钮的背景改成这个drawable文件

       <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/circle_button"
            android:text="Button"/>
      

      如果你想要一个完整的圆形按钮,你可以使用下面的 drawable

      <?xml version="1.0" encoding="utf-8"?>
      <shape
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:shape="oval">
      
          <solid
              android:color="#008577"/>
      
          <size
              android:width="120dp"
              android:height="120dp"/>
      </shape>
      

      【讨论】:

        【解决方案8】:

        如果您不想更改整个应用的主题。您可以专门为此视图使用材质主题:

        <com.google.android.material.button.MaterialButton
            android:id="@+id/fooButon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            android:fontFamily="sans-serif"
            android:padding="8dp"
        ==> android:theme="@style/Theme.MaterialComponents.Light"
            app:backgroundTint="@color/base_white" />
        

        【讨论】: