首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >按钮(有时)不会绘制StateList xml背景

按钮(有时)不会绘制StateList xml背景
EN

Stack Overflow用户
提问于 2012-03-15 16:08:54
回答 2查看 468关注 0票数 1

我的应用程序中有一个向导,用户可以使用" back“和"Next”按钮在活动之间来回移动。这些按钮对于向导中的每个活动都是相同的,并在它们自己的xml布局中定义,这些xml布局包含在每个活动的布局中。他们有一个状态列表作为他们的背景,这通常是可行的,除非偶尔会发生这样的情况:

预期:

实数:

按钮定义- add_nav_buttons.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rlAddNav"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        orientation="horizontal">
        <Button
            android:id="@+id/bAddBack"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:background="@drawable/b_add_nav_button"
            android:text="@string/bBack">
        </Button>
        <Button
            android:id="@+id/bAddNext"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:background="@drawable/b_add_nav_button"
            android:text="@string/bNext">
        </Button>
    </LinearLayout>
</RelativeLayout>

包括示例- some_activity_layout.xml

代码语言:javascript
复制
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rlAddMain"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <!-- OTHER STUFF -->

    <include
        android:id="@+id/NavButtons"
        layout="@layout/add_nav_buttons"
        android:layout_marginTop="-50dp"
        android:layout_alignParentBottom="true">
    </include>
</RelativeLayout> 

StateList定义- b_add_nav_button.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false">
        <shape>
            <gradient
                android:startColor="#C7C7C7"
                android:endColor="#8B8B8B"
                android:angle="270">
            </gradient>
            <stroke
                android:width="1dp"
                android:color="#8B8B8B"> 
            </stroke>
            <padding
                android:left="10dp"
                android:right="10dp"
                android:top="10dp"
                android:bottom="10dp">
            </padding>
        </shape>
    </item>
    <item android:state_pressed="true" >
        <shape>
            <solid
                android:color="#FFA319" />
            <padding
                android:left="10dp"
                android:right="10dp"
                android:top="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
    <item>
        <shape>
            <gradient
                android:startColor="#4F4FB2"
                android:endColor="#24248F"
                android:angle="270">
            </gradient>
            <stroke
                android:width="1dp"
                android:color="#24248F"> 
            </stroke>
            <padding
                android:left="10dp"
                android:right="10dp"
                android:top="10dp"
                android:bottom="10dp">
            </padding>
        </shape>
    </item>
</selector>

我完全困惑,任何帮助都将不胜感激。只是为了重新迭代,大多数情况下,预期的行为会发生,只是偶尔有一次背景画不出来。

我还应该补充说,这些按钮在功能上仍然像他们应该做的那样工作。

编辑

我添加了一些调试语句并成功地进行了再现。显然,安卓有时会将b_add_bav_button.xml解释为ColorDrawable而不是StateListDrawable。我尝试将形状从状态列表中提取到它们自己的xml文件中,而不是骰子。我还是很困惑。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-03-19 03:42:36

安卓认为我使用的是ColorDrawable而不是StateListDrawable,这导致了上面提到的行为。我仍然不知道为什么,但这与屏幕方向有关(它可以通过启动活动、弹出活动、旋转屏幕和重新启动活动来复制)。

我的工作是将xml选择器中的s从xml选择器中提取出来,而是以编程方式构建一个StateListDrawable,如下所示:

代码语言:javascript
复制
StateListDrawable d = new StateListDrawable();
d.addState(new int[]{ android.R.attr.state_pressed, android.R.attr.state_enabled }, 
           this.getResources().getDrawable(R.drawable.b_add_nav_button_pressed));
d.addState(new int[]{ -android.R.attr.state_enabled },
           this.getResources().getDrawable(R.drawable.b_add_nav_button_disabled));
d.addState(new int[]{ android.R.attr.state_enabled },
           this.getResources().getDrawable(R.drawable.b_add_nav_button_normal));
bNext.setBackgroundDrawable(d);

有很多奇怪的事情正在发生,但我似乎让它起作用了。

票数 2
EN

Stack Overflow用户

发布于 2012-06-07 02:05:36

将一个文件"a_dummy_magic.xml“作为”最顶层“的可绘制资源-它可以固定在位置。只有Android有这种不合理的行为。请相信我。我经历了这一切,花了整整一周的时间来寻找这个窍门。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9723648

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档