【问题标题】:Aligning the vertical center of a view to the top of another view将视图的垂直中心与另一个视图的顶部对齐
【发布时间】:2015-12-30 13:38:56
【问题描述】:

这看起来相当简单,但我似乎可以想办法生成实现以下结果的 xml:(SO 上有很多有趣的内容,但令人惊讶的是,我没有找到任何答案)

黑色视图是外框,蓝色视图与底部对齐,红色视图的垂直中心与蓝色视图顶部对齐。

约束:

  • 不要假设红色视图具有固定大小(计算它的一半作为偏移边距),
  • 不要以编程方式进行,xml rulez

编辑:修复了混乱的描述并添加了约束

【问题讨论】:

  • 水平居中是什么意思?两个视图似乎都覆盖了根视图的整个宽度。你能在显示对齐的图像上放置一个标记吗?
  • @tasomaniac 抱歉,我修复了混乱的描述和图片
  • 我会考虑如何在xml中做到这一点。它可以很容易地以编程方式完成。将视图放在另一个视图的顶部,并在一半高度的情况下给出一个负的底部边距。
  • 其中一个有固定大小吗?或者我可以在活动中做吗?
  • 确实,在 xml 中硬编码大小或以编程方式执行它会起作用,但我排除了它。 (只是在描述中添加了约束)

标签: android android-layout


【解决方案1】:

如果您不能指望有一个固定的高度,那么边距需要在运行时确定高度时确定。否则,您硬编码一个底部边距,它是布局高度(蓝色布局)的一半的负值。我提供了两个嵌套布局供您在主活动布局中使用。

在这种特殊情况下,我使用了一个按钮来测试应用程序,您可以选择任何方式来实现它。将布局添加到 xml 的顺序将影响它们的可见性。重要的是把你想要的放在最后。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout ../..
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/black"
            tools:showIn="@layout/activity_main">

    <RelativeLayout android:id="@+id/l1"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_alignParentBottom="true"
        android:background="@android:color/holo_blue_light"/>

    <RelativeLayout android:id="@+id/l2"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_above="@+id/l1"
        android:background="@android:color/holo_red_light"/>
</RelativeLayout>

我从this answer here借了一个方法:

public void setMargin(View view) {
    if (relativeLayout
            .getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
        ViewGroup.MarginLayoutParams marginLayoutParams =
                (ViewGroup.MarginLayoutParams) relativeLayout
                        .getLayoutParams();
        int margin = relativeLayout.getHeight() / 2;
        marginLayoutParams.setMargins(20, 0, 0, -margin);
        relativeLayout.requestLayout();
    }
}

出于演示的目的,我在两个布局的任一侧推了边距,以便您可以看到它们重叠的位置。

【讨论】:

    【解决方案2】:

    如果浮动视图具有固定大小。

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ImageView
            android:id="@+id/a"
            android:layout_alignParentBottom="true"
            android:background="#44bb11"
            android:layout_width="match_parent"
            android:layout_height="100dp" />
        <ImageView
            android:layout_marginBottom="-40dp"
            android:layout_above="@+id/a"
            android:id="@+id/c"
            android:background="#45000000"
            android:layout_width="match_parent"
            android:layout_height="80dp" />
    </RelativeLayout>
    

    【讨论】:

    • 确实,在 xml 中硬编码大小会起作用,但我排除了它。 (只是在描述中添加了约束)
    猜你喜欢
    • 1970-01-01
    • 2020-06-03
    • 1970-01-01
    • 2017-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-10
    • 1970-01-01
    相关资源
    最近更新 更多