Layout_Below 未在 relativelayout 中正确执行

Layout_Below is not getting executed properly in relativelayout

我有以下 xml 代码。 imagebutton 没有在 textView 下方对齐,边距为 5dp。我想我在理解各种布局参数时犯了一个错误。相对布局是线性布局的子视图,几乎没有其他视图。

        <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="40">

        <com.github.lzyzsd.circleprogress.DonutProgress
            android:id="@+id/amain_dp"
            android:layout_width="240dp"
            android:layout_height="240dp"
            android:layout_centerInParent="true" />

        <TextView
            android:id="@+id/amain_tv_currentsteps"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:gravity="center"
            android:textSize="25sp" />

        <ImageButton
            android:id="@+id/amain_ib_whatsapp"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginTop="5dp"
            android:layout_centerHorizontal="true"
            android:layout_below="@id/amain_tv_currentsteps"
            android:background="@drawable/whatsapp_icon"/>

    </RelativeLayout>

这是我得到的输出:

编辑:如果删除 layout_weight 参数并提供固定的 layout_height 参数,代码将正常工作。但是想了解为什么上面的代码因为 layout_weight

而失败

编辑:完成 xml 代码:

<ScrollView 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="in.jiyofit.basic_app.MainActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/amain_tv_target"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="20dp"
        android:layout_weight="10" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="40">

        <com.github.lzyzsd.circleprogress.DonutProgress
            android:id="@+id/amain_dp"
            android:layout_width="240dp"
            android:layout_height="240dp"
            android:layout_centerInParent="true" />

        <TextView
            android:id="@+id/amain_tv_currentsteps"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:gravity="center"
            android:textSize="25sp" />

        <ImageButton
            android:id="@+id/amain_ib_whatsapp"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginTop="5dp"
            android:layout_centerHorizontal="true"
            android:layout_below="@id/amain_tv_currentsteps"
            android:background="@drawable/whatsapp_icon"/>

    </RelativeLayout>

    <FrameLayout
        android:id="@+id/amain_fl_container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="30"
        android:layout_marginTop="20dp"/>

</LinearLayout>

您需要使用 + 像这样:

 android:layout_below="@+id/amain_tv_currentsteps"

在你的顶部 RelativeLayout 给它一个大小 0dp 会造成问题,因为它(rel 的高度为 0 并且)你的图像视图低于它。

android:layout_height="match_parent"

鉴于您的布局具有以下方案:

<ScrollView>
    <LinearLayout>
        <TextView/>
        <RelativeLayout/>
        <FrameLayout/>
    </LinearLayout>
</ScrollView>

在上面的层次结构中,所有LinearLayout children 都有一个布局权重属性 设置,基本上要求parent 提供(layout_weight/weightSum) * parentHeight() space 在 parent 和 LinearLayout 本身有一个 lauout_height 设置为 wrap_content 这基本上要求 children 告诉他们的高度所以 LinearLayout 本身可以相应地缩放。这是一个循环依赖,因此布局工具无法确定位置。

在这种情况下,您有两个选择:

1 - 删除 ScrollView 并将 Linearlayout's 高度设置为 match_parent。布局的每个 child 元素都会相应地拉伸以正确占据屏幕 space。您的布局将如下所示:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="80">

    <TextView
        android:id="@+id/amain_tv_target"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="20dp"
        android:layout_weight="10"/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="40">

        <com.github.lzyzsd.circleprogress.DonutProgress
            android:id="@+id/amain_dp"
            android:layout_width="240dp"
            android:layout_height="240dp"
            android:layout_centerInParent="true"/>

        <TextView
            android:id="@+id/amain_tv_currentsteps"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:gravity="center"
            android:textSize="25sp"/>

        <ImageButton
            android:id="@+id/amain_ib_whatsapp"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_below="@id/amain_tv_currentsteps"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="5dp"/>

    </RelativeLayout>

    <FrameLayout
        android:id="@+id/amain_fl_container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="20dp"
        android:layout_weight="30"/>

</LinearLayout> 

2 - 因为你有一个 ScrollView,你可以为 LinearLayout 的每个 child 提供绝对高度,并让 LinearLayout 高度为 wrap_content. ScrollView 将针对不同的屏幕相应地适当缩放您的布局。您的布局可能如下所示:

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weightSum="80">

        <TextView
            android:id="@+id/amain_tv_target"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginTop="20dp"/>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="400dp">

            <com.github.lzyzsd.circleprogress.DonutProgress
                android:id="@+id/amain_dp"
                android:layout_width="240dp"
                android:layout_height="240dp"
                android:layout_centerInParent="true"/>

            <TextView
                android:id="@+id/amain_tv_currentsteps"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:gravity="center"
                android:textSize="25sp"/>

            <ImageButton
                android:id="@+id/amain_ib_whatsapp"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:layout_below="@id/amain_tv_currentsteps"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="5dp"/>

        </RelativeLayout>

        <FrameLayout
            android:id="@+id/amain_fl_container"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_marginTop="20dp"/>

    </LinearLayout>

</ScrollView>

根据经验,如果可以确定 LinearLayout 的 height/width,然后再将 layout_weight 属性 提供给 child仁。希望这有帮助。