我有以下 xml 代码。 imagebutton 没有在 textView 下方对齐,边距为 5dp。我想我在理解各种布局参数时犯了一个错误。相对布局是线性布局的 subview ,几乎没有其他 View 。
<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>
最佳答案
鉴于您的布局具有以下方案:
<ScrollView>
<LinearLayout>
<TextView/>
<RelativeLayout/>
<FrameLayout/>
</LinearLayout>
</ScrollView>
在上面的层次结构中,所有 LinearLayout
子级都有一个布局权重属性集,基本上要求父级为其提供 (layout_weight/weightSum) * parentHeight()
空间在父级中,LinearLayout
本身有一个 lauout_height
设置为 wrap_content
,它基本上要求 child 说出他们的高度,所以 LinearLayout
本身可以相应地扩展。这是一个循环依赖,因此布局工具无法确定位置。
在这种情况下,您有两个选择:
1 - 删除 ScrollView
并将 Linearlayout 的
高度设置为 match_parent
。布局的每个子元素都会相应地拉伸(stretch)以正确占用屏幕空间。您的布局将如下所示:
<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
的每个子级提供绝对高度,并让 LinearLayout
高度为包装内容
。 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>
根据经验,在向其子项提供 layout_weight
属性之前,请确保您的 LinearLayout
的高度/宽度是可确定的。希望这会有所帮助。
关于android - Layout_Below 未在 relativelayout 中正确执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38684447/