在父母之外去的时候Android视图消失了

Era*_*ray 61 java animation android android-animation

我在这个LinearLayout中有一个LinearLayout和ImageView.

ImageView有一个翻译效果.

// v = ImageView    
ObjectAnimator animation2 = ObjectAnimator.ofFloat(v, "translationY", 200);
                        animation2.setDuration(3000);
                        animation2.setTarget(v);
                        animation2.start();
Run Code Online (Sandbox Code Playgroud)

动画工作但是当ImageView超出LinearLayout时它就消失了.

你可以在这里看到问题:http://screenr.com/zoAH

如何在不修改LinearLayout高度的情况下修复它.

小智 80

找到ImageView所属的ViewGroup并应用ViewGroup.setClipChildren(false).默认情况下,子项的绘制仅限于父ViewGroup的边界.

  • 我已经将`clipChildren ="false"设置为ImageView的所有父级(我的意思是LinearLayout的父级).它正在发挥作用.谢谢. (56认同)
  • 在XML上设置LinearLayout的`android:clipChildren ="false"`但没有改变? (4认同)
  • 如果还没有向下滚动,请参阅下面的maxwell的答案.设置`android:clipToPadding ="false"`. (4认同)

Max*_*ell 75

存在两个可能导致此情况发生的属性:clipChildren和clipToPadding.对于每个父ViewGroup,您需要将clipChildren设置为false,该ViewGroup将对象进行动画处理.您还需要将clipToPadding设置为直接父级(可能更多,但我还没有看到它的情况).

您可以在XML中设置这两个属性

android:clipChildren="false"
android:clipToPadding="false"
Run Code Online (Sandbox Code Playgroud)

或者在代码中

viewGroup.setClipChildren(false);
viewGroup.setClipToPadding(false);
Run Code Online (Sandbox Code Playgroud)

  • `每个父ViewGroup`都是关键!谢谢你的回答. (9认同)
  • 我个人也不得不在每个父母身上将clipToPadding设置为false.奇怪的. (3认同)

ahm*_*_89 16

我的实施.它可能可以帮助某人:

Java版本:

public static void setAllParentsClip(View v, boolean enabled) {
    while (v.getParent() != null && v.getParent() instanceof ViewGroup) {
        ViewGroup viewGroup = (ViewGroup) v.getParent();
        viewGroup.setClipChildren(enabled);
        viewGroup.setClipToPadding(enabled);
        v = viewGroup;
    }
}
Run Code Online (Sandbox Code Playgroud)

调用 setAllParentsClip(yourView, false);以禁用所有父项中的剪辑.

编辑:

Kotlin的版本作为扩展功能:

fun View.setAllParentsClip(enabled: Boolean) {
    var parent = parent
    while (parent is ViewGroup) {
        parent.clipChildren = enabled
        parent.clipToPadding = enabled
        parent = parent.parent
    }
}
Run Code Online (Sandbox Code Playgroud)

呼叫: yourView.setAllParentsClip(false)