是否可以将相对布局的子项对齐到另一个子项的顶部?

Is it possible to align a child of a relative layout centered on top of another child?

我有一个相对布局,其中包含一些文本视图和一个(圆形)图像视图,图像视图居中。

有一个从相对布局的中心发出的展开环的动画,当图像视图也居中时,它也从图像视图的中心发出。

但是现在需要将图像视图垂直向上移动一点,使其不再位于布局的中心,因此动画的原点也需要垂直移动相同的程度。

这是动画实现方式的草图,它只显示了一小部分代码来展示其实现的主要功能。

public class RippleBackground extends RelativeLayout {
private ArrayList<RippleView> rippleViewList=new ArrayList<RippleView>();
    private Paint paint;
    private AnimatorSet animatorSet;
    private ArrayList<Animator> animatorList; 

        ...
        paint = new Paint();
        rippleParams=new LayoutParams((int)(2*(rippleRadius+rippleStrokeWidth)),(int)(2*(rippleRadius+rippleStrokeWidth)));
        rippleParams.addRule(CENTER_IN_PARENT, TRUE);

        animatorSet = new AnimatorSet();
        animatorSet.setInterpolator(new AccelerateDecelerateInterpolator());
        animatorList=new ArrayList<Animator>();
        addView(rippleView,rippleParams);
        rippleViewList.add(rippleView);

        ...
        animatorSet.playTogether(animatorList);


    private class RippleView extends View {
        @Override
        protected void onDraw(Canvas canvas) {
            int radius=(Math.min(getWidth(),getHeight()))/2;
            canvas.drawCircle(radius,radius,radius-rippleStrokeWidth,paint);
        }
    }

这是目前的实施方式,我想知道我是否可以做一些事情,例如用其他规则替换 rippleParams.addRule(CENTER_IN_PARENT, TRUE),但我看不到任何其他适用的规则。

我想知道是否可以使用规则来对齐动画视图,使其不再以父 RelativeLayout 为中心,而是以 RelativeLayout 内的 imageView 为中心?

或者是否可以指定坐标,或者我可以将动画的原点向上移动以匹配 imageView 中心的其他方式?

和你一样,我看不出你如何用一个简单的规则来做到这一点,但你可以做的是用一些 paddingBottom 将你的 RippleView 包裹在 FrameLayout 中在它上面匹配你的 ImageView 从中心向上移动的量。

例如,如果您的 ImageView 现在位于 RelativeLayout 的中心上方 100dp,您还可以将 paddingBottom 设置为 100dp 在新的 FrameLayout 上包装你的 RippleView 为了更清楚地表达我的意思,这里是实现它的 XML 布局(我只使用了我认为的 XML它更清楚发生了什么,我相信您能够轻松地将其转换为在您的代码中以编程方式完成):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MyActivity">

    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#0f0"
        android:layout_centerInParent="true"
        android:paddingBottom="100dp"
        >
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher"
            android:background="#f00"/>
    </FrameLayout>
</RelativeLayout>

我已经为 FrameLayoutImageView 添加了背景颜色(在你的情况下就是你的 RippleView)所以你可以清楚地看到它们的边界 - 请注意ImageView 完全包含在 FrameLayout:

另一种选择是将 ImageViewRippleView 放在 FrameLayout 中,宽度和高度设置为 wrap_content,然后将 FrameLayout 放置 ImageView 并设置 layout_gravity="center" on yourRippleView, so that theRippleViewends up centred on theImageViewby virtue of the fact the bounds of theFrameLayoutmatch the bounds of theImageView` - 例如:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MyActivity">

    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#0f0"
        android:layout_centerVertical="true"
        >
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher"
            android:background="#f00"/>

        <View
            android:id="@+id/rippleView"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:background="#00f"
            android:layout_gravity="center"
            />
    </FrameLayout>
</RelativeLayout>