Вопрос

I am setting the default look of all buttons in my application as follows. When I do this, nothing happens on click of the buttons, even though I have defined all functions correctly. In fact, when I comment out the line <item name="android:buttonStyle">@style/button</item>, the button clicks work fine (but of course, they use the default android style). Application theme is also defined in the manifest: android:theme="@style/AppTheme" Can someone please tell me why this is happening? Thanks.

button_shape.xml in res/drawable:

<?xml version="1.0" encoding="UTF-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">

<corners
    android:radius="10dp"   />

<gradient 
    android:angle="90"
    android:startColor="#6AA4ED"
    android:endColor="#927BED"/>

<padding
    android:left="10dp"
    android:right="10dp"
    android:top="12dp"
    android:bottom="12dp" />
</shape>

styles.xml:

<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="AppBaseTheme" parent="android:Theme.Light">
</style>

<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:buttonStyle">@style/button</item>
</style>

<style name="button">
    <item name="android:background">@drawable/button_shape</item>
</style>

Это было полезно?

Решение

Change your

<style name="button">
    <item name="android:background">@drawable/button_shape</item>
</style>

to

<style name="button" parent="@android:style/Widget.Button">
    <item name="android:background">@drawable/button_shape</item>
</style>

adding the correct parent property will make you buttons click-able.

For different states of button you can use

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" >
        <shape>
            <gradient
                android:startColor="@color/MyButtonDarkGray"
                android:endColor="@color/MyButtonLightGray"
                android:angle="270" />
            <stroke
                android:width="0dp"
                android:color="@color/Gray" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>

    <item android:state_focused="true" >
        <shape>
            <gradient
                android:endColor="@color/LightGreen"
                android:startColor="@color/DarkGreen"
                android:angle="270" />
            <stroke
                android:width="0dp"
                android:color="@color/Gray" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>

    <item>        
        <shape>
            <gradient
                android:endColor="@color/LightGreen"
                android:startColor="@color/DarkGreen"
                android:angle="270" />
            <stroke
                android:width="0dp"
                android:color="@color/Gray" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>

</selector>
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top