首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >StateListDrawable行为怪异

StateListDrawable行为怪异
EN

Stack Overflow用户
提问于 2012-10-08 16:17:34
回答 1查看 473关注 0票数 0

我有这个StateListDrawable:

代码语言:javascript
复制
GradientDrawable gd = new GradientDrawable(Orientation.TOP_BOTTOM, new int[] {
            firstColor, secondColor });
StateListDrawable sld = new StateListDrawable();
sld.addState(new int[] { android.R.attr.state_pressed },
            new ColorDrawable(onClickColor));
sld.addState(StateSet.WILD_CARD, gd);

这是我的布局:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android" >

    <RelativeLayout
        android:id="@+id/actionBar"
        android:layout_width="fill_parent"
        android:layout_height="48dp"
        android:baselineAligned="true" 
        android:descendantFocusability="afterDescendants">

        <ImageButton
            android:id="@+id/actionButtonBack"
            android:layout_width="48dip"
            android:layout_height="48dip"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true" />

        <ImageButton
            android:id="@+id/actionButton3"
            android:layout_width="48dip"
            android:layout_height="48dip"
            android:layout_alignParentTop="true"
            android:layout_toLeftOf="@+id/actionButton2" />

        <ImageButton
            android:id="@+id/actionButton2"
            android:layout_width="48dip"
            android:layout_height="48dip"
            android:layout_alignParentTop="true"
            android:layout_alignWithParentIfMissing="true"
            android:layout_toLeftOf="@+id/actionButton1" />

        <ProgressBar
            android:id="@+id/actionButton1"
            android:layout_width="48dip"
            android:layout_height="48dip"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_alignWithParentIfMissing="true"
            android:padding="4dip" />

        <TextView
            android:id="@+id/tvTitle"
            android:layout_width="wrap_content"
            android:layout_height="48dip"
            android:layout_alignParentTop="true"
            android:layout_alignWithParentIfMissing="true"
            android:layout_toLeftOf="@+id/actionButton3"
            android:layout_toRightOf="@+id/actionButtonBack"
            android:gravity="center"
            android:textSize="14dip"
            android:textStyle="bold" />
    </RelativeLayout>

</merge>

然后我像这样设置可抽屉:

代码语言:javascript
复制
tv.setClickable(false);
tv.setTextColor(textViewColor);
actionBack.setBackgroundDrawable(sld);
action2.setBackgroundDrawable(sld);
action3.setBackgroundDrawable(sld);
tv.setBackgroundDrawable(sld);
pb.setBackgroundDrawable(sld);

当我点击actionBack按钮时,整个布局就变成了我的onClickButton颜色。

我只想让它的行为就像我按下了一个常规xml定义的可绘制按钮一样。

我做错了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-10-08 16:24:01

这是因为'tv‘的layout_width (带有id android:id=“@+id/tvTitle”的TextView )是'wrap_content’,所以当一个颜色被应用到这个视图时,它需要整个边界,你要么需要硬编码TextView的layout_width,要么将颜色应用为具有硬编码宽度的ShapeDrawable。希望这能解决你的问题。

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

https://stackoverflow.com/questions/12777745

复制
相关文章

相似问题

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