代码之家  ›  专栏  ›  技术社区  ›  Expiredmind

绘制自定义椭圆形和矩形XML形状作为按钮背景

  •  0
  • Expiredmind  · 技术社区  · 7 年前

    我想创建如下所示的自定义按钮背景:

    enter image description here

    corners 矩形中的属性 shape 因为它只环绕拐角,而左侧的中间是直的,或者当我对该属性设置太多值时,它看起来是这样的。
    enter image description here

    我希望我能结合两种形状(椭圆形和长方形),如图所示 here 但每当我尝试旋转图层列表时,它看起来不像是想要的形状。

    2 回复  |  直到 7 年前
        1
  •  3
  •   Community Mohan Dere    5 年前

    试着这样做:

                 <Button
                    android:layout_width="100dp"
                    android:layout_height="wrap_content"
                    android:background="@drawable/test"
                    android:text="Button"
                    android:textColor="@color/black"
                  />
    

    @drawable/test 将是这样的:

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item
            android:left="0dp"
            android:right="-100dp"
            android:top="-30dp"
            android:bottom="-30dp">
    
            <shape android:shape="oval">
                <solid
                    android:color="@android:color/white" />
            </shape>
        </item>
    
    
    </layer-list>
    

    注: 我知道这不是最好的解决办法。但就目前而言,这会有所帮助。 如果您想增加按钮的宽度,请相应地更改以下属性以满足您的需要。

    android:top="-30dp"
    android:bottom="-30dp"
    

    result 将如下所示:

    enter image description here

        2
  •  0
  •   Pang Ajmal PraveeN    7 年前

    使用下面的代码作为可绘制文件,并设置为按钮的背景。对于曲线边,可以从下方增加或减少半径。

    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <stroke
            android:width="3dp"
            android:color="#ffffff" />
        <corners android:radius="10dp" />
        <gradient
            android:angle="270"
            android:endColor="#2b74d2"
            android:startColor="#2b74d2"
            android:type="linear" />
    </shape>
    
    推荐文章