使用按钮标签,我可以自定义按钮吗?

时间:2013-05-30 00:21:27

标签: java android eclipse

我想使用我为我的应用程序绘制的自定义按钮图像,但是这样做我需要使用不同的图像来处理聚焦的按钮,而另一个则按下它。

我遇到了选择器标签但由于某种原因它不喜欢它。 Eclipse抱怨“渲染库已损坏”。我得到的错误是:

Broken rendering library; unsupported DPI. Try using the SDK manager to get updated.

我有,我已经更新了每个过去10的API。如果重要,我的目标API是15,我的编译API是17。

如果我无法使用它,我可以简单地使用Button标签,也许可以在Java src代码中更改它吗?

2 个答案:

答案 0 :(得分:0)

使用像这样的自定义布局

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/pressed"></item>

    <item android:state_focused="true" android:drawable="@drawable/focus"></item>

    <item android:drawable="@drawable/normal"></item>
</selector>

还要注意选择器应该以这种特定方式定义,否则它们会产生问题.i.e。

1)state_pressed
2)state_focused ( work only if you scroll to that button using the hardware key)
3)drawable i.e. normal

如果您更改了选择器的顺序,那么它将无法工作。记住它的一个简单方法是可视化qwerty手机 - 首先我看到按钮(normal),然后使用箭头键(state_focused)移动到该特定按钮,然后按下该按钮({{ 1}})。现在把它们写回来。

答案 1 :(得分:0)

而不是按钮,创建ImageView并在点击图像时执行必要的操作。

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="Method_Name"
        android:src="@drawable/selector" >
    </ImageView>

另外,为了在点击和焦点上提供不同的图像,在drawable文件夹中创建selector.xml并将imageview的背景设置为选择器文件。

selector.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android" >

     <item android:drawable="@drawable/image1"  android:state_focussed="true" />
     <!-- Inactive tab -->
    <item android:drawable="@drawable/image2"  android:state_pressed="true" />
     <!-- Pressed tab -->


     </selector>

希望这会对你有帮助!

相关问题