【问题标题】:Using the button tag, can I customize the button?使用按钮标签,我可以自定义按钮吗?
【发布时间】:2013-05-25 10:17:44
【问题描述】:

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

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

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

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

如果我不能让它工作,我可以简单地使用 Button 标记并在 Java src 代码中更改它吗?

【问题讨论】:

  • 你试过清理项目并重新启动eclipse吗?另外,一些代码会很好。
  • @a_schimpf 代码没什么大不了的。它不会抛出任何错误或异常。所以它确实编译,但它崩溃了。我得到的唯一错误是损坏的库错误。
  • 你是在模拟器还是手机上运行代码?没有选择器标记代码,一切都运行良好吗?你在 xml 中拼写正确吗—— xmlns:android="schemas.android.com/apk/res/android"?
  • @a_schimpf 我在手机上运行代码。它总是崩溃,是的,它只是与选择器标签有关。当我使用按钮标签或 ImageButton 标签时没有问题,但不是我需要的。一切都写得对,我用的是 Eclipse,所以很多都是模板。
  • 这里有人有同样的错误信息。它看起来像一个可能的修复。 code.google.com/p/android/issues/detail?id=37472

标签: java android eclipse


【解决方案1】:

使用这样的自定义布局

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

还要注意,选择器应该以这种特殊方式定义,否则它们会出现问题。即

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) 移动到该特定按钮,然后我按下该按钮 (state_pressed)。现在倒着写。

【讨论】:

    【解决方案2】:

    而不是按钮,创建 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的背景设置为选择器文件。

    选择器.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>
    

    希望对您有所帮助!

    【讨论】:

      猜你喜欢
      • 2022-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-03
      • 2018-06-26
      • 1970-01-01
      • 1970-01-01
      • 2017-04-26
      相关资源
      最近更新 更多