Android阴影位于RelativeLayout的底部超出边界

Android阴影位于RelativeLayout的底部超出边界,android,android-layout,android-imageview,android-image,Android,Android Layout,Android Imageview,Android Image,我有一个9面片的阴影图像,我想添加到RelativeLayout的底部。布局填满屏幕,但向上滑动,然后用户点击一个按钮。因此,我希望阴影图像位于RelativeLayout(向下拉,底部边距为负数)的下方,这样当布局向上时,阴影位于布局的底部边缘,使其具有分层效果 <ImageView android:id="@+id/shadow" android:layout_width="fill_parent" android

我有一个9面片的阴影图像,我想添加到
RelativeLayout
的底部。布局填满屏幕,但向上滑动,然后用户点击一个按钮。因此,我希望阴影图像位于
RelativeLayout
(向下拉,底部边距为负数)的下方,这样当布局向上时,阴影位于布局的底部边缘,使其具有分层效果

<ImageView
            android:id="@+id/shadow"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:layout_marginBottom="-10dp"
            android:src="@drawable/shadow" />
奇怪的是,当我将图像添加到布局中并给它一个负的边距时,它只是没有显示出来,就像它在剪切布局边界之外的任何东西一样


有办法解决这个问题吗?

我可以想出一个办法,但我相信一定有更干净的办法

将mainView所在的布局设置为FrameLayout(或RelativeLayout),并将ImageView包含在其中,使其成为mainView的同级,但将其列在mainView之前。使用
layout\u gravity=“bottom”
(或
layout\u alignParentBottom=“true”
如果使用RelativeLayout,则将其设置为底部)

现在,将ObjectAnimator中的目标更改为main视图上方的某个对象(即其容器视图或活动/片段),并将属性更改为类似“scrollUp”的内容,然后在目标中创建一个名为
setScrollUp(float)
的方法。在此方法中,使用
setTranslationY()
设置主视图和阴影的平移。将阴影偏移其高度

在一个简单的应用程序中使用纯色为我工作:

XML:


这取决于你如何向上滑动。你能添加更多关于这个的细节吗?@nmw用代码更新了我的问题。谢谢。最后,我在动画停止的位置设置了阴影静态。动画是如此之快,以至于我认为用户甚至不会注意到阴影没有随着帧向上滑动。
ObjectAnimator mover = ObjectAnimator.ofFloat(mainView, "translationY", !historyShown ? -ph : 0);
 mover.setDuration(300);
 mover.start();
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:background="#f00">

    <View
        android:id="@+id/shadow"
        android:layout_width="match_parent"
        android:layout_height="20px"
        android:layout_gravity="bottom"
        android:background="#00f"/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#0f0"
        android:id="@+id/container"/>

</FrameLayout>
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    container = findViewById(R.id.container);
    shadow = findViewById(R.id.shadow);

    container.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(final View v)
        {
            final float translationTo = (open) ? 0 : -300;
            final float translationFrom = (open) ? -300 : 0;

            open = !open;

            ObjectAnimator anim = ObjectAnimator.ofFloat(MyActivity.this, "scrollUp", translationFrom, translationTo);
            anim.setDuration(500);
            anim.start();
        }
    });
}

public void setScrollUp(final float position)
{
    container.setTranslationY(position);
    shadow.setTranslationY(position + shadow.getHeight());
}