ChatGPT解决这个技术问题 Extra ChatGPT

如何使按钮的角变圆?

我想制作 button 轮的角。有没有一种简单的方法可以在 Android 中实现这一点?

这不是一个广泛的问题,它绝对是重点。将其标记为“太宽泛”只是一种需要改变的心态。别再做独裁者了。
同意user734028:太宽泛了怎么会被关闭?如果 OP 询问如何将角半径设置为 N 像素,则唯一的方法可能会更具体。来吧!
2020 年,Google 开发了新的 UI 框架“JetPack Compose”:代码更少、直观、加速开发且功能强大我认为它是“SwiftUI”的替代品Jetpack Compose

D
Daniel Lerps

如果你想要这样的东西

https://i.stack.imgur.com/3YUOI.png

这是代码。

1.在您的可绘制文件夹中创建一个 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>

2.现在使用这个drawable作为你视图的背景。如果视图是按钮,则如下所示:

<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" />

它崩溃了:原因:org.xmlpull.v1.XmlPullParserException:二进制 XML 文件第 24 行: 标签需要“drawable”属性或定义可绘制对象的子标签
什么会在点击时产生阴影?我正在尝试减少底部的阴影宽度......没有运气
我看不出显示选择器的编码方式与解释按钮角的编码方式有什么关系。
什么是“android:state_focused”项目?我认为这与手机无关。
@Harsha,也许当您输入一些文本并且键盘布局上有输入键按钮时。所以你必须决定默认点击哪个 Button 视图,Ok 或 Cancel 或其他。否则似乎没有必要
S
Sandip Jadhav

在drawable文件夹中创建一个xml文件,如下所示

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" android:padding="10dp">
    <!-- you can use any color you want I used here gray color-->
    <solid android:color="#ABABAB"/> 
    <corners android:radius="10dp"/>
</shape>

将此作为背景应用到要使角变圆的按钮上。

或者您可以为每个角落使用单独的半径,如下所示

android:bottomRightRadius="10dp"
android:bottomLeftRadius="10dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp"

您可以将角缩短为 android:radius="10dp",这将适用于所有
这不是一个完整的解决方案,因为它不支持各种按钮状态(按下、聚焦、默认)。如需更好的解决方案,请参阅 stackoverflow.com/questions/9334618/rounded-button-android
@BenSimpson,当您只放置一条线而不是单独定义每个角半径时,您会发现形状有所不同。
G
Gabriele Mariotti

有没有一种简单的方法可以在 Android 中实现这一点?

是的,今天有,而且非常简单。
只需使用带有 app:cornerRadius 属性的 Material Components library 中的 MaterialButton

就像是:

    <com.google.android.material.button.MaterialButton
        android:text="BUTTON"
        app:cornerRadius="8dp"
        ../>

https://i.stack.imgur.com/lBsy8.png

获得一个圆角的按钮就足够了。

您可以使用材料按钮 styles 之一。例如:

<com.google.android.material.button.MaterialButton
    style="@style/Widget.MaterialComponents.Button.OutlinedButton"
    .../>

https://i.stack.imgur.com/Lia0e.png

此外,从 1.1.0 版本开始,您还可以更改按钮的 shape。只需在按钮样式中使用 shapeAppearanceOverlay 属性:

  <style name="MyButtonStyle" parent="Widget.MaterialComponents.Button">
    <item name="shapeAppearanceOverlay">@style/ShapeAppearanceOverlay.MyApp.Button.Rounded</item>
  </style>

  <style name="ShapeAppearanceOverlay.MyApp.Button.Rounded" parent="">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSize">16dp</item>
  </style>

然后只需使用:

<com.google.android.material.button.MaterialButton
   style="@style/MyButtonStyle"
   .../>

您还可以在 xml 布局中应用 shapeAppearanceOverlay

<com.google.android.material.button.MaterialButton
   app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.MyApp.Button.Rounded"
   .../>

shapeAppearance 还允许每个角具有不同的形状和尺寸:

<style name="ShapeAppearanceOverlay.MyApp.Button.Rounded" parent="">
    <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>

https://i.stack.imgur.com/OSldg.png

通过 Jetpack Compose,您可以使用 shape 参数:

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

https://i.stack.imgur.com/VouSh.png


它还需要使用 Material 主题
@Vlad 是的,材质组件需要材质主题。
P
Pavithra Purushothaman

创建一个如下所示的 XML 文件。将其设置为按钮的背景。如果您需要更多的按钮曲线,请将半径属性更改为您的愿望。

button_background.xml

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

为您的按钮设置背景:

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/button_background"/>

A
Almostafa

在可绘制文件夹中创建 shape.xml

像 shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
  <stroke android:width="2dp"
    android:color="#FFFFFF"/>
  <gradient 
    android:angle="225"
    android:startColor="#DD2ECCFA"
    android:endColor="#DD000000"/>
<corners
    android:bottomLeftRadius="7dp"
    android:bottomRightRadius="7dp"
    android:topLeftRadius="7dp"
   android:topRightRadius="7dp" />
</shape>

在 myactivity.xml 中

您可以使用

<Button
    android:id="@+id/btn_Shap"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:text="@string/Shape"
    android:background="@drawable/shape"/>

C
Community

创建文件 myButton.xml

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

添加到您的按钮

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

A
AntonioSanchez

我发现的简单方法是在可绘制文件夹中创建一个新的 xml 文件,然后将按钮背景指向该 xml 文件。这是我使用的代码:

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

<solid android:color="#ff8100"/>
<corners android:radius="5dp"/>

</shape>

要在自定义可绘制背景中恢复 Material-theme 波纹效果,请在 Button View 中添加 android:foreground="?attr/selectableItemBackground"。请参阅stackoverflow.com/questions/38327188/…
R
Ravi Makvana

在 Drawable 文件夹中创建 rounded_btn.xml 文件...

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
     <solid android:color="@color/#FFFFFF"/>    

     <stroke android:width="1dp"
        android:color="@color/#000000"
        />

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

     <corners android:bottomRightRadius="5dip" android:bottomLeftRadius="5dip" 
         android:topLeftRadius="5dip" android:topRightRadius="5dip"/> 
  </shape>

并使用 this.xml 文件作为按钮背景

<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/rounded_btn"
android:text="Test" />

A
A.G.THAMAYS

https://i.stack.imgur.com/qw29Z.png

   <Button
        android:id="@+id/buttonVisaProgress"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="5dp"
        android:background="@drawable/shape"
        android:onClick="visaProgress"
        android:drawableTop="@drawable/ic_1468863158_double_loop"
        android:padding="10dp"
        android:text="Visa Progress"
        android:textColor="@android:color/white" />

形状.xml

    <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="14dp" />
<gradient
    android:angle="45"
    android:centerColor="#1FA8D1"
    android:centerX="35%"
    android:endColor="#060d96"
    android:startColor="#0e7e1d"
    android:type="linear" />
<padding
    android:bottom="0dp"
    android:left="0dp"
    android:right="0dp"
    android:top="0dp" />
<size
    android:width="270dp"
    android:height="60dp" />
<stroke
    android:width="3dp"
    android:color="#000000" />


D
Deep Singh Baweja

此链接包含您需要的所有信息。 Here

形状.xml

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

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

    <corners    android:bottomLeftRadius="8dip"
                android:topRightRadius="8dip"
                android:topLeftRadius="1dip"
                android:bottomRightRadius="1dip"
                />
</shape>

和 main.xml

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent">

    <TextView   android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Hello Android from NetBeans"/>

    <Button android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Nishant Nair"
            android:padding="5dip"
            android:layout_gravity="center"
            android:background="@drawable/button_shape"
            />
</LinearLayout>

这应该会给你想要的结果。

祝你好运


b
bikram

如果您想更改拐角半径并希望在按下按钮时产生涟漪效果,请使用:-

将 button_background.xml 放入 drawable

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="#F7941D">

    <item android:id="@android:id/mask">
        <shape android:shape="rectangle">
            <solid android:color="#F7941D" />
            <corners android:radius="10dp" />
        </shape>
    </item>

    <item android:id="@android:id/background">
        <shape android:shape="rectangle">
            <solid android:color="#FFFFFF" />
            <corners android:radius="10dp" />
        </shape>
    </item>

</ripple>

将此背景应用于您的按钮

<Button
    android:background="@drawable/button_background"
    android:id="@+id/myBtn"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:text="My Button" />

这不仅比公认的答案简单得多,而且涟漪效应提供了更清晰的用户体验。
H
Hamdy Abd El Fattah

可绘制文件夹

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#FFFFFF"/>
    <corners android:radius="30dp"/>
    <stroke android:width="2dp" android:color="#999999"/>
</shape>

布局文件夹

<Button
    android:id="@+id/button2"
    <!-- add style to avoid square background -->
    style="@style/Widget.AppCompat.Button.Borderless"
    android:background="@drawable/corner_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />

确保添加样式以避免方形背景


M
Mark Allison

如果您使用的是矢量绘图,那么您只需要指定一个 <corners>可绘制定义中的元素。我在 blog post 中对此进行了介绍。

如果您使用的是位图/9-patch 可绘制对象,那么您需要在位图图像中创建具有透明度的角。


D
Dharman

普通的 Button 标记有一个 app:cornerRadius 属性。

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:backgroundTint="#009688"
    android:onClick="xyz"
    android:paddingHorizontal="64dp"
    android:text="@string/login"
    app:cornerRadius="32dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/passwordCustom"
    app:layout_constraintVertical_bias="0.1" 
    />

这值得一票
V
Vander Ig

您还可以使用如下卡片布局

  <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="60dp"
        app:cardCornerRadius="30dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"

            >

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="Template"

                />

        </LinearLayout>

    </androidx.cardview.widget.CardView>

坏主意。因为问题是关于圆形按钮
A
Aditya Shinde

这是一个简单的 CardView,使用它可以使 Button 的角变为圆形。

<androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardCornerRadius="30dp"
        android:background="#fff"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp">
        <Button
            android:id="@+id/button_login"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Login"
            android:textSize="20sp"
            app:cornerRadius="32dp"/>

    </androidx.cardview.widget.CardView>

您的答案可以通过额外的支持信息得到改进。请edit添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。您可以找到有关如何写出好答案的更多信息in the help center
A
ABU Ali14Q

image

<androidx.cardview.widget.CardView
        android:id="@+id/add_coment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="15dp"
        android:clickable="true"
        android:foreground="?selectableItemBackground"
        android:outlineAmbientShadowColor="@color/blue_shadow_outline"
        app:cardCornerRadius="25dp"
        app:layout_constraintBottom_toBottomOf="@+id/coment"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@+id/coment">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_gravity="center_horizontal"
            android:background="@drawable/btn_style2"
            android:fontFamily="@font/ar1"
            android:paddingHorizontal="10dp"
            android:text="text "
            android:textColor="@color/text_color"
            android:textSize="17dp" />
    </androidx.cardview.widget.CardView>

您的答案可以通过额外的支持信息得到改进。请edit添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。您可以找到有关如何写出好答案的更多信息in the help center
我认为这不是正确的解决方案。对于可视化界面,这没关系。但在功能上两者都不相同。
S
SAZL

在 drawable 中创建 XML 文件并将按钮背景设置为此文件。

XML文件代码例如:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!--|^@^|_[ Shadows ]_|^@^|-->
    <item>
        <shape>
            <padding android:top="2dp" android:right="2dp" android:bottom="2dp" android:left="2dp" />
            <gradient android:angle="315" android:startColor="#c2c2c2" android:endColor="#c0c0c0"/>
            <corners android:radius="3dp" />
        </shape>
    </item>
    <!--|^@^|_[ Background ]_|^@^|-->
    <item>
        <shape>
            <gradient android:angle="135" android:startColor="#f7f7f7" android:endColor="#fbfcfc"/>
            <corners android:radius="3dp" />
        </shape>
    </item>
</layer-list>