【问题标题】:Button doesn't change its shape in selector按钮在选择器中不会改变其形状
【发布时间】:2015-07-12 11:34:33
【问题描述】:

我正在尝试自定义按钮的外观。 问题是,那个按钮不想改变它的形状。这意味着它仍然是尖锐的而不是椭圆形的。

button_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@color/green_bright">
        <shape xmlns:android="http://schemas.android.com/apk/res/android">
            <corners android:radius="8dp"/>
            <stroke android:color="@color/green_primary"/>
            <padding android:bottom="4dp" android:left="4dp" android:right="4dp" android:top="4dp"/>
        </shape>
    </item>
    <item android:drawable="@color/green">
        <shape xmlns:android="http://schemas.android.com/apk/res/android">
            <corners android:radius="8dp"/>
            <stroke android:color="@color/green_primary"/>
            <padding android:bottom="4dp" android:left="4dp" android:right="4dp" android:top="4dp"/>
        </shape>
    </item>
</selector>

my_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:paddingLeft="8dp"
    android:paddingRight="8dp"
    android:paddingTop="8dp"
    android:paddingBottom="8dp"
    android:orientation="vertical">    
<Button android:id="@+id/button_id"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:text="@string/button_text"
    android:textColor="@color/grey_dark"
    android:background="@drawable/button_selector"/>
</LinearLayout>

已解决

问题是我在 item

中声明了 drawable
<item android:drawable="@color/..."/>

它设置了固定的背景,它使背景不可变,所以任何形状都不能改变按钮的属性。背景颜色在中设置。

<solid android:color="@color/blue"/>

重要!您可以使用可绘制来代替颜色,但所有您的纯色都需要可绘制

<solid android:drawable="@color/blue"/>

更正了下面的 button_selector.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">
            <solid
                android:color="@color/blue_bright"/>
            <corners
                android:radius="20dp"/>
            <stroke
                android:color="@color/blue_primary"
                android:width="2dp"/>
            <padding
                android:bottom="4dp" android:left="4dp" android:right="4dp" android:top="4dp"/>
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <solid
                android:color="@color/blue"/>
            <corners
                android:radius="20dp"/>
            <stroke
                android:color="@color/blue_primary"
                android:width="2dp"/>
            <padding
                android:bottom="4dp" android:left="4dp" android:right="4dp" android:top="4dp"/>
        </shape>
    </item>

</selector>

同样重要的是,您可以在 Button 中使用您的图片,而不仅仅是将其放入:

<Button>
  ...
  android:src="@drawable/myPicture"
</Button>

【问题讨论】:

    标签: android xml button selector shape


    【解决方案1】:

    使用纯色填充颜色并为描边添加宽度。 像这样的:

    <item>
        <shape android:shape="rectangle">
            <solid android:color="#ff22cc84"/>
            <corners android:radius="8dp"/>
            <stroke android:width="2dp" android:color="#ff800424"/>
            <padding android:bottom="4dp" android:left="4dp" android:right="4dp" android:top="4dp"/>
        </shape>
    </item>
    

    【讨论】:

    • 没有任何改变。所以.. 背景颜色效果很好,但形状保持不变。我添加了&lt;shape android:shape:rectangle&gt;,但这改变了一切。我什至不知道银弹在哪里。
    • 形状保持不变(没有圆角)是什么意思?将半径增加到更高的值,例如 20dp。
    • 我的意思是形状保持锐利/矩形。增加半径不起作用。你认为错误是在 @drawable/button_selector 之外的地方吗?
    猜你喜欢
    • 1970-01-01
    • 2018-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-20
    相关资源
    最近更新 更多