安卓:子元素,即使duplicateParentState指定他们的父母分享pressed状态 [英] Android: Child elements sharing pressed state with their parent even when duplicateParentState specified

查看:293
本文介绍了安卓:子元素,即使duplicateParentState指定他们的父母分享pressed状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含其中包含一些按钮子元素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

这篇关于安卓:子元素,即使duplicateParentState指定他们的父母分享pressed状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆