【问题标题】:Rounded corners of Button [duplicate]按钮的圆角[重复]
【发布时间】:2013-02-17 06:56:02
【问题描述】:

基本上,据我所知,当您为按钮添加背景颜色时,圆角元素会消失,因此基本上没有圆角。但是当我没有背景颜色时,按钮的角是圆形的。

我不明白发生了什么。 请帮忙

【问题讨论】:

    标签: android button


    【解决方案1】:

    边框是默认背景可绘制对象的一部分,因此如果您想要的话,您也必须将其替换为包含边框的边框。 采用默认背景资源并对其进行编辑以具有一些颜色。 或者,您可以使用

        button = (Button) findViewById(R.id.button);
        button.getBackground().setColorFilter(color, Mode.SRC_ATOP);
    

    这可能会提供更接近您期望的东西。

    【讨论】:

      【解决方案2】:
          <?xml version="1.0" encoding="utf-8"?>
              <selector xmlns:android="http://schemas.android.com/apk/res/android" >
                      <item android:state_enabled="false">
                          <shape xmlns:android="http://schemas.android.com/apk/res/android"
                              android:padding="10dp" android:shape="rectangle">
                              <solid android:color="#FFb888"/>
                              <corners android:bottomLeftRadius="15dp" android:bottomRightRadius="15dp" android:topLeftRadius="15dp" android:topRightRadius="15dp"/>
                          </shape>            
                      </item>
      
                      <item android:state_enabled="true" android:state_pressed="true">
                          <shape xmlns:android="http://schemas.android.com/apk/res/android"
                              android:padding="10dp" android:shape="rectangle">
                              <solid android:color="#FFD488"/>
                              <corners android:bottomLeftRadius="15dp" android:bottomRightRadius="15dp" android:topLeftRadius="15dp" android:topRightRadius="15dp"/>
                          </shape>            
                      </item>
      
                      <item android:state_enabled="true">
                          <shape xmlns:android="http://schemas.android.com/apk/res/android"
                              android:padding="10dp" android:shape="rectangle">
                              <solid android:color="#FF8C00"/>
                              <corners android:bottomLeftRadius="15dp" android:bottomRightRadius="15dp" android:topLeftRadius="15dp"    android:topRightRadius="15dp"/>
                          </shape>            
                      </item>
              </selector>
      
          <Button
          android:id="@+id/btn_add_list"
          android:layout_width="125dp"
          android:layout_height="wrap_content"
          android:background="@drawable/rounded_orange_button"
          android:text="@string/action_new_list"
          android:textColor="@color/white"
          android:gravity="center"
          android:textSize="18sp"
          android:layout_marginTop="20dp"
          android:layout_marginRight="10dp"
          android:layout_alignParentRight="true"               
          android:onClick="doNewList"/>
      

      您可以将上述将成为按钮背景的 xml 文件保存在 res/drawable 文件夹中名为 rounded_orange_button.xml 的文件中。所以它将是 res/drawable/rounded_orange_button.xml。这也会在用户选择按钮时向用户提供按钮反馈,更接近 developer.android.com 的设计指南,但这是您的偏好。给猫剥皮的方法有一百万种。然后通过第二个示例引用您在 your_layout.xml 文件中的可绘制文件夹中创建的内容。颜色值是可选的,如果需要,也可以从 res/values/colors.xml 中的另一个资源文件中设置。将 res/values/strings.xml 用于您希望在按钮内显示的任何字符串。希望这可以帮助。此外,按钮上的曲线量可以通过调整

      中的半径值来调整

      【讨论】:

        【解决方案3】:

        按钮的背景将为您提供方角。对于一个漂亮的自定义背景,我建议制作背景图像并用作按钮的背景或 src。所以你的 xml 看起来像:

        android:src="@drawable/mybuttonimage"
        android:bacgkround="@null"
        

        android:background="@drawable/mybuttonimage"
        

        我会测试两者,因为它们可以以不同的方式拉伸您的图像。

        另一种选择是在 res/drawable 文件夹中创建一个形状。

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

        【讨论】:

        • 这有效,但按钮背景颜色为黑色。如何将其更改为绿色
        • 我编辑了帖子,使其变为绿色。将&lt;solid android:color="#33DDFF" /&gt; 更改为另一种颜色代码以更改绿色。查看色卡here
        猜你喜欢
        • 2012-09-28
        • 2016-02-29
        • 2019-05-17
        • 2011-05-04
        • 2013-10-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多