Android 举止古怪

Android 举止古怪,android,statelistdrawable,Android,Statelistdrawable,我有一个可绘制的状态列表: GradientDrawable gd = new GradientDrawable(Orientation.TOP_BOTTOM, new int[] { firstColor, secondColor }); StateListDrawable sld = new StateListDrawable(); sld.addState(new int[] { android.R.attr.state_pressed },

我有一个可绘制的状态列表:

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);
这是我的布局:

<?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>
当我点击actionBack按钮时,整个布局就变成了onClickButton的颜色

我只是希望它的行为就像我按下了一个常规的xml定义的按钮一样


我做错了什么?

这是因为“tv”(id为android:id=“@+id/tvTitle”)的布局宽度是“包装内容”,所以当颜色应用于此视图时,它会占用整个边界,您需要硬编码TextView的布局宽度,或者将颜色应用为具有硬编码宽度的可形状。希望这能解决您的问题。

问题在于它没有固定的宽度。只要布局需要,进度条和按钮都可以隐藏。我已经试过你让我做的了。现在,当我单击一个按钮时,TextView bg变为空。然而,当我切换屏幕方向时,它会恢复到它应该的状态。是的,边界是在运行时应用的,而不是硬编码的。为了便于尝试,只需将的layout_width设置为某个倾角值,然后查看它是否有效。然后告诉我结果,或者更确切地说,如果你提供屏幕截图,它会很有帮助。最好的部分是,它的行为因API版本而异。在姜饼中,当我按下按钮时,tv drawable变为空。在ICS中,可拉伸保持不变。。。即使我点击按钮。我的意思是,当按钮被点击时,没有任何变化。
tv.setClickable(false);
tv.setTextColor(textViewColor);
actionBack.setBackgroundDrawable(sld);
action2.setBackgroundDrawable(sld);
action3.setBackgroundDrawable(sld);
tv.setBackgroundDrawable(sld);
pb.setBackgroundDrawable(sld);