【问题标题】:Layout changes are not reflecting in the application布局更改未反映在应用程序中
【发布时间】:2011-07-27 16:35:12
【问题描述】:

我通过引用this 文章创建了一个带有自定义图像的按钮小部件。我已经为 onclick 和 onfocus 设置了图像。但是我只能看到按钮的正常图像,当我聚焦或按下按钮时没有任何变化。

这是我的代码:

custom_button.xml:

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/ic_reload_4" />
    <item 
        android:drawable="@drawable/ic_reload_2"
        android:state_pressed="true" />
    <item 
        android:drawable="@drawable/ic_reload_3"
        android:state_focused="true" />
</selector>

布局:

<Button 
    android:id="@+id/reload_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:background="@drawable/custom_button" />

图像放置在可绘制文件夹中。我不确定为什么它不起作用。我也遇到了布局问题,即我可以在选择“布局”选项卡时看到日食中的变化,但更新后它没有显示在应用程序中。谁能帮我解决这个问题...

【问题讨论】:

    标签: android layout button android-layout imagebutton


    【解决方案1】:

    你的选择器应该是这样的:

        <?xml version="1.0" encoding="UTF-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item 
            android:drawable="@drawable/ic_reload_2"
            android:state_pressed="true" />
        <item 
            android:drawable="@drawable/ic_reload_3"
            android:state_focused="true" />
     <item android:drawable="@drawable/ic_reload_4" />
    </selector>
    

    即您必须将默认的item 放在最后。看来 android 像 switch-case 块一样膨胀这个。

    【讨论】:

    • 谢谢!!.. 这是我的错误,我没有完全遵循文档:)
    • 我还注意到,在某些情况下,更改会显示在 Eclipse(布局选项卡)中,但不会显示在应用程序中。例如:我在一个布局中有 3 个标题。我已经使用 strings.xml 中定义的颜色值设置了所有 3 个标题的背景,我可以看到 eclipse 中的更改,但在我的应用程序中,更改应用于第一个标题。你有什么想法吗?
    猜你喜欢
    • 2011-01-11
    • 2016-12-12
    • 2012-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多