【问题标题】:How Do Create This Type Of Button In Android如何在 Android 中创建这种类型的按钮
【发布时间】:2014-04-05 08:36:58
【问题描述】:

如何在android中创建这种类型的按钮。我试过了 。我可以知道实现目标的正确方法是什么吗?也许这个问题太基础了,但我没有找到任何合适的解决方案。请帮帮我。

我想创建这个

               <Button
                android:id="@+id/Btn1"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:background="@drawable/green_circle"
                android:textSize="20sp"
                android:textColor="@android:color/white"
                android:clickable="true"
                />

这里是 green_circle.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
     <stroke android:width="1sp" android:color="#54d66a" />
</shape>

当我使用这个时,按钮看起来

我怎样才能得到我的愿望按钮。任何帮助表示赞赏

【问题讨论】:

标签: android xml button


【解决方案1】:

你定义了 1sp(你应该使用“dp”顺便说一句。)圈子,你明白了。 我认为实现您想要的最简单的方法是使用图层列表 xml 元素 - 只需将当前绘图作为第一层并将第二层作为中心点(只是带有一些边距的实心椭圆) 这里有文档:http://developer.android.com/guide/topics/resources/drawable-resource.html#LayerList 简单的用法(这不是你的情况,但我认为它告诉你应该做什么):

<layer-list >
    <item>
        <shape android:shape="rectangle">
            <corners android:radius="2dp"/>
            <solid android:color="@color/grey"/>
        </shape>
    </item>
    <item android:bottom="2dp">
        <shape android:shape="rectangle">
            <corners android:radius="2dp"/>
            <solid android:color="@color/green_light"/>
        </shape>
    </item>
</layer-list>

关键是android:bottom="2dp",导致前景层小于背景层。

【讨论】:

  • 先生:您的方法是正确的。这就是为什么我赞成您的回答。将形状“矩形”添加到“椭圆”并更新顶部、右侧、按钮、左侧的半径字段,如 .顺便说一句,非常感谢您的关注。祝您编码愉快
  • 是的,我知道 - 只是我在当前打开的项目中有这个例子;)
【解决方案2】:

实现所需效果的一种方法是使用 Layer List Drawable。要获得与您发布的图像相似的 Drawable,请尝试以下操作

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="oval">
            <stroke android:width="1sp" android:color="#54d66a" /> 
        </shape>
    </item>
    <item android:top="5dp" android:left="5dp" android:right="5dp" android:bottom="5dp">
        <shape android:shape="oval">
            <solid android:color="#54d66a" />
        </shape>
    </item>
</layer-list>

【讨论】:

  • 是的。我已经做到了。你的方法太正确了(y)。顺便说一句,我接受你的回答。快乐编码
【解决方案3】:

1.使用两个不同的收音机图像打开和关闭你想要的,并在drawable中制作一个xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_checked="false" android:drawable="@drawable/check_off" />
     <item android:state_checked="true" android:drawable="@drawable/checkbox_on" />
     <item android:drawable="@drawable/checkbox_off" />
</selector>

2. 将此可绘制对象设置为单选按钮的背景。

【讨论】:

    【解决方案4】:

    使用ImageButton

    <ImageButton android:src="@drawable/green_circle" />
    

    文档还说明了如何根据按钮的状态拥有不同的图像。

    【讨论】:

      【解决方案5】:

      将绿色圆圈的图像放入可绘制文件夹中。然后在 xml 中为您的按钮设置背景图片

             <Button
                  android:id="@+id/Btn1"
                  android:layout_width="20dp"
                  android:layout_height="20dp"
                  android:background="@drawable/yourimage"
                  />
      

      这会起作用:)

      【讨论】:

      • 如果你检查我的代码,你会发现我避免使用 IMAGE 。尝试使用样式创建自定义按钮。
      • 按钮背景很简单,那你为什么要找这么复杂的方式??
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多