【问题标题】:Can't click button after setting style设置样式后无法点击按钮
【发布时间】:2014-04-04 20:34:55
【问题描述】:

我正在设置应用程序中所有按钮的默认外观,如下所示。当我这样做时,即使我已经正确定义了所有功能,单击按钮也不会发生任何事情。事实上,当我注释掉<item name="android:buttonStyle">@style/button</item> 行时,按钮点击工作正常(当然,它们使用默认的android 样式)。应用程序主题也在清单中定义:android:theme="@style/AppTheme" 有人可以告诉我为什么会这样吗?谢谢。

res/drawable 中的button_shape.xml:

<?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>

【问题讨论】:

  • 为什么不将样式应用为按钮本身的背景,看看它是否有效:)
  • 当我使用 android:background="@drawable/button_shape" 作为按钮时,它工作正常。但我想对所有按钮都这样做,而不必把这条线放在任何地方。为什么会发生这种情况??
  • 你是添加点击监听还是在xml中定义点击方法?
  • XML。使用 android:onClick。是这个问题吗?

标签: android android-button android-drawable android-styles


【解决方案1】:

改变你的

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

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

添加正确的父属性将使您的按钮可以点击。

你可以使用不同的按钮状态

<?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>

【讨论】:

  • button_shape 没有任何问题,因为如果我使用 android:background="@drawable/button_shape",一切正常,样式和点击一样。
  • 你是对的,没问题,有错字。休息一下,我会再次检查并更新您
  • 是的!非常感谢!很棒的是,它不会弄乱具有可绘制对象的按钮:)
  • button_shape.xml 中需要一点格式,第一个是在android:shape="rectangle" 之前添加一个空格,最后关闭&lt;/shape&gt; 不显示,因为它缺少代码格式。你问了一个很好的问题,它肯定会帮助别人。
  • 哦,是的,我已经这样做了,但是在粘贴时它搞砸了。非常感谢!哦,顺便说一句,您之前发布了带有颜色状态的 button_shape 代码。我看到我现在需要它。请问可以发在这里吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多