安卓:子元素,即使duplicateParentState指定他们的父母分享pressed状态他们的、元素、父母、状态

2023-09-03 22:20:49 作者:一个即将堕落的小伙

我有一个包含其中包含一些按钮子元素RelativeLayout的元素SlidingDrawer元素:

I have a SlidingDrawer element which contains a RelativeLayout element which contains some Button child elements:

<SlidingDrawer>
  <RelativeLayout>
    <LinearLayout>
      <Button android:background="@drawable/foo.xml" android:duplicateParentState="false">
      <Button android:background="@drawable/bar.xml" android:duplicateParentState="false">
    </LinearLayout>
  </RelativeLayout>
</SlidingDrawer>

foo.xml和bar.xml有选择适用根据国家不同的图像:

foo.xml and bar.xml have selectors which apply different images depending on the state:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_focused="true" android:drawable="@drawable/foo_selected" />
  <item android:state_pressed="true" android:drawable="@drawable/foo_selected" />
  <item android:state_enabled="false" android:drawable="@drawable/foo_disabled" />
  <item android:drawable="@drawable/foo_normal" /> 
</selector>

我看到的问题是,当我点击滑动抽屉把手,在pressed状态被触发的按钮,他们看起来pssed太$ P $,即使我指定了 duplicateParentState 为false。

推荐答案

重写的LinearLayout 类的子类。在该子类覆盖pssed 方法设置$ P $,什么也不做,像这样:

Override the LinearLayout class with a subclass. In that subclass override the setPressed method and do nothing like so:

public class UnpressableLinearLayout extends LinearLayout
{
    @Override
    public void setPressed(boolean pressed)
    {
        // Do nothing here. Specifically, do not propagate this message along
        // to our children so they do not incorrectly display a pressed state
        // just because one of their ancestors got pressed.
    }
}

替换的LinearLayout 联合国pressableLinearLayout