【问题标题】:how to make a capsule shape Button in android?如何在android中制作胶囊形状的按钮?
【发布时间】:2014-12-30 10:26:07
【问题描述】:

我想做一个和这张图片一模一样的按钮

我想使用一个 xml 文件来生成这样的按钮。谁能告诉我该怎么做?

【问题讨论】:

标签: android button android-button


【解决方案1】:

我终于找到了使用 xml 文件的方法。这是给我胶囊形状按钮的 xml 文件的代码。

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

  <corners
    android:bottomLeftRadius="30dp"
    android:bottomRightRadius="30dp"
    android:radius="60dp"
    android:topLeftRadius="30dp"
    android:topRightRadius="30dp" />

  <solid android:color="#CFCFCF" />

  <padding
    android:bottom="0dp"
    android:left="0dp"
    android:right="0dp"
    android:top="0dp" />

  <size
    android:height="60dp"
    android:width="270dp" />

</shape>    

【讨论】:

  • radius 属性将被忽略,因为您还设置了单独的半径(例如 bottomLeftRadius)。引用docs,“所有角的半径,作为尺寸值或尺寸资源。以下属性会覆盖每个角的半径。”
  • 我已经移除了静态尺寸属性并将公共半径增加到50 dp(移除所有顶部、底部、左侧和右侧半径后)
  • 如果您希望形状在任何视图尺寸下保持正确,只需将半径设置为 10000dp
  • 解决方案如此复杂。有关更多简单优雅解决方案,请参阅stackoverflow.com/a/51947130/3940133
【解决方案2】:

只需通过 cornerRadius 属性和您喜欢的宽度使用 MaterialButton

 <com.google.android.material.button.MaterialButton
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            app:cornerRadius="18dp"
            android:text="BUTTON"
            />

您也可以使用 shapeAppearanceOverlay 属性:

<com.google.android.material.button.MaterialButton
        app:shapeAppearanceOverlay="@style/buttomShape"
        .../>

与:

  <style name="buttomShape">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSize">50%</item>
  </style>

【讨论】:

  • 这应该是使用 Material Components 进行现代 android 开发的新接受的答案
  • 如何告诉这个“智能”小部件无论其高度是多少,都始终是胶囊状的,而没有像给它app:cornerRadius="GOOGOLdp" 这样的蹩脚的变通办法?
  • @Farid 我已经更新了答案。您可以使用 shapeAppearanceOverlay 属性,使用 50% 的cornerSize。
【解决方案3】:

我没有在这里尝试所有的答案,但我相信其中一些/全部都是有效的。 接受的答案也有效,但可以简化。由于我喜欢优雅简单的解决方案,我想出了自己的解决方案。基本上,与View 的宽度和高度相比,我们只需要添加足够大的半径,这样它就会创建一个圆角。在这种情况下,我输入1000dp 以确保安全。我们甚至根本不需要添加android:shape="rectangle"

只需执行以下 2 个简单步骤:

  1. 在您的可绘制文件夹中创建一个 XML 文件。例如,我们将其命名为bg.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <corners android:radius="1000dp"/>
    
        <solid android:color="@color/yourPreferredColor"/>
    </shape>
    
  2. 然后您可以在布局 XML 文件中使用它作为您的 View 属性 android:background="@drawable/bg" 或者直接在代码view.setBackgroundResource(R.drawable.bg)

【讨论】:

  • 按钮与上述代码结合使用。
【解决方案4】:

考虑为其定制一个shape 并在该形状内使用corners

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="10dp"/> <!-- increasing the value, increases the rounding. And as  TTransmit said, to make it like a capsule make the radius half of your button height -->
    <solid android:color="#AAAAAA"/> <!-- the button color -->

</shape>

因此,将该形状保存在您的 /drawable 文件夹中,假设它将保存为“button_bg.xml”,因此在布局 xml 中声明 Button 时:

<Button
    android:background="@drawable/button_bg"
    android:layout_height="20dp"
                .
                .                          />

【讨论】:

  • 获得胶囊形状的技巧是另外将按钮元素的高度指定为角半径的两倍,在这种情况下为 20dp。使用高度和可绘制背景一起制作样式将是一个不错的选择。
  • 我尝试了上述使用矩形和椭圆形的方法,但似乎没有给出我想要的胶囊形状。
  • @user3393926 那么,你得到了什么?
  • @user3393926 如果您的形状是圆形但不够圆,看起来像胶囊,请尝试 TTransmit 建议的手动将 Button 的高度设置为半径值的两倍或尝试将它们组合在一起如果您没有固定按钮的值,则为样式
  • @NeonWarge 在 dp 中指定值是在 Android 中指定大小值的最佳实践,在像小按钮这样的情况下,指定一个固定值不会有任何害处,但是在完整的情况下布局,指定 dp 值的最佳实践是为每个密度指定值或为每个尺寸指定值,例如将值命名为“dp_test”并在 values-sw600dp/dimens 和 values-sw720dp/dimens 中稍作不同的更改来指定它
【解决方案5】:

它被称为材料中的芯片,可以这样使用:

<com.google.android.material.chip.Chip
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world"/>

更多信息可以在herehere找到。

要在您的项目中使用 Material 组件,您需要将适当的库添加到您的 build.gradle:

dependencies {
    // ...
    implementation 'com.google.android.material:material:1.0.0-beta01'
    // ...
  }

更多关于向您的项目添加材料的信息可以在here找到。

或者,您可以使用最新版本的支持设计库:

<android.support.design.chip.Chip
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:chipText="@string/hello_world"/>

同时拉入适当的库:

dependencies {
    // ...
    implementation 'com.android.support:appcompat-v7:28.0.0-alpha1'
    implementation 'com.android.support:design:28.0.0-alpha1'
    // OR
    implementation 'com.android.support:design-chip:28.0.0-alpha1'
    // ...
}

有关后一种方法的更多信息,请参阅this answer

【讨论】:

  • 不错!我相信这可以简化Android开发。但是恕我直言,这太过分了,因为它有很多功能,我们甚至不需要创建一个简单的胶囊形按钮。
【解决方案6】:

这是在 xml 中创建按钮的代码,但是如果你想将按钮创建为胶囊形状,则必须添加背景

        <Button
            android:id="@+id/image"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            **android:background="@drawable/button_background"**
            android:text="@string/image" >
        </Button>

在drawable文件夹中创建button_background.xml,在button_background.xml中写入如下代码

         <?xml version="1.0" encoding="UTF-8"?>

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

             android:shape="rectangle" android:padding="40dp">

          <!-- you can use any color you want I used here gray color-->

         <solid android:color="#01A9DB"/>

          <corners

          android:bottomRightRadius="20dp"

          android:bottomLeftRadius="20dp"

          android:topLeftRadius="20dp"

          android:topRightRadius="20dp"/>

      </shape>

【讨论】:

    【解决方案7】:

    试试下面,适用于任何视图大小:

    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
    
        <corners android:radius="1000dp" />
    
        <solid android:color="@android:color/black" />
    
    </shape>
    

    【讨论】:

      猜你喜欢
      • 2020-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-03
      相关资源
      最近更新 更多