Android - 将视图中心与其他视图的底部对齐

Jos*_*nTD 16 android view alignment

一张图片讲述的不仅仅是冗长的演讲: 在此输入图像描述

我想垂直对齐红色部分的中心和黑色部分的中间.我没有容器的约束(RelativeLayout,FrameLayout,LinearLayout oO).

我试着用0dp对准红鉴于它的黑色视图和alignBaseline底部的高度查看,但它不工作...

谢谢你的帮助 !

Bry*_*yan 26

Android现在支持布局锚点CoordinatorLayout:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <View android:id="@+id/black_view"
        android:layout_width="match_parent" android:layout_height="@dimen/black_height"/>

    <View android:id="@+id/red_view"
        android:layout_width="@dimen/red_width" android:layout_height="@dimen/red_height"
        app:layout_anchor="@id/black_view" app:layout_anchorGravity="bottom|center"/>

</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)

如果更改Java代码中视图的大小,锚也将保持不变.

  • 这真是最好的答案.CoordinatorLayout允许JosselinTD尝试实现的视图的精确放置. (2认同)
  • 不起作用!它将红色视图与黑视图的底部对齐,但是它与100%重叠,而不仅仅是50%的重叠! (2认同)

Ada*_*331 23

这也可以使用ConstraintLayout.类似于将视图的开始/结束与父级中心对齐的方式,我们可以使用该概念沿视图的边缘居中.

这种布局的关键是底层视图的两个约束:

app:layout_constraintTop_toBottomOf="@id/top_view"
app:layout_constraintBottom_toBottomOf="@id/top_view"
Run Code Online (Sandbox Code Playgroud)

通过将下视图的顶部/底部约束到上部视图的底部,布局将调整为使其沿着底部居中.以下是蓝图的完整代码和屏幕截图:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <View
        android:id="@+id/top_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/green"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <View
        android:id="@+id/bottom_view"
        android:layout_width="58dp"
        android:layout_height="58dp"
        android:background="@color/red"
        app:layout_constraintBottom_toBottomOf="@id/top_view"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/top_view" />

</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • 哦,我一直在寻找它3天.谢谢 (2认同)

Jos*_*nTD 3

最后,我使用了一种更加编程化的方式来解决这个问题,因为View的大小不是固定的。

这里是解决方案:

布局 :

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <View
                android:id="@+id/black"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"/>

            <View
                android:id="@+id/red"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true">
        </RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

代码 :

            red.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    red.getViewTreeObserver().removeOnGlobalLayoutListener(this);

                    LayoutParams params = new LayoutParams(
                            LayoutParams.MATCH_PARENT,      
                            LayoutParams.WRAP_CONTENT
                            );
                    params.setMargins(0, 0, 0, red.getHeight()/2);
                    black.setLayoutParams(params);
                }
            });
Run Code Online (Sandbox Code Playgroud)

感谢您的帮助 !它帮助我找到了这个!