android - 为什么我不能将按钮更改为圆形按钮?

标签 android

我在资源上创建了 xml 文件 - 文件名 button_shape.xml (android studio 将此文件添加到 res/values )

我将此代码添加到 xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    android:shape="rectangle">
    <gradient android:startColor="#2D8CD6"
        android:endColor="#2D8CD6"
        android:angle="270"/>

    <corners android:bottomRightRadius="0dp"
        android:bottomLeftRadius="25dp"
        android:topLeftRadius="0dp"
        android:topRightRadius="25dp"/>
</shape>
</resources>

(如果我从 xml 中删除 => 我得到一个错误)

现在,我将其添加到代码中

<Button
        android:text="Start"
        android:layout_column="0"
        android:layout_columnWeight="1"
        android:layout_margin="10dp"
        android:background="@drawable/button_shape"
        />

但是我在

上遇到了错误

"android:background="@drawable/button_shape""

android studio 不允许我将 xml 位置更改为“drawable”下

  1. 我做错了什么?
  2. 如何将简单按钮变成圆形按钮?

最佳答案

将此 XMl 添加到 Drawable 文件夹中

round_button.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="false">
        <shape android:shape="oval">
            <solid android:color="#ffb5a6"/>
        </shape>
    </item>
    <item android:state_pressed="true">
        <shape android:shape="oval">
            <solid android:color="#ffeddd"/>
        </shape>
    </item>
</selector>

调用xml

  <Button
                android:id="@+id/button"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:background="@drawable/round_button"
                android:text="-"
                android:textColor="#fff"/>

关于android - 为什么我不能将按钮更改为圆形按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33688917/

相关文章:

java - 在 Android 中下载图像

Android 网格形成

android - 如何使用 Intellij IDE(最好是 Android Studio)导航/查看/编辑 android 平台源代码(AOSP)?

android - NotificationCompat 无法解析为类型

android - 音频索引 : Generating time-aligned text tags from audio file

android - 无法在未调用 Looper.prepare() 的线程内创建处理程序

java - 如何使用此特定属性以编程方式创建 TextView?

android - Genymotion 在 android studio 中不显示日志

android - 我们可以使用 TextView 在 Android 上创建图书应用吗?

android - 共享首选项不起作用,android?