RelativeLayout是否支持嵌套?嵌套的子RelativeLayout及内部控件无法显示如何解决
问题原因
你代码中的三个Button存在相对布局循环依赖,RelativeLayout在测量布局时无法确定三个按钮的位置,最终导致整个子RelativeLayout测量失败,自身背景和内部控件都无法渲染。
具体循环依赖逻辑如下:
delete_button配置了android:layout_toLeftOf="@id/next_button",依赖next_button的位置确定自身位置next_button同时配置了android:layout_toRightOf="@id/delete_button"和android:layout_toLeftOf="@id/next_10_button",同时依赖另外两个按钮的位置next_10_button配置了android:layout_toRightOf="@id/next_button",反过来依赖next_button的位置
解决方案
给其中一个按钮添加和父RelativeLayout绑定的锚点属性,打破循环依赖即可。
示例修改方案(让三个按钮整体靠父布局右侧排列):
<RelativeLayout android:id="@+id/functional_buttons" android:layout_width="match_parent" android:layout_height="50dp" android:layout_marginTop="250dp" android:background="#DC143C"> <!-- 先定义最右侧按钮,锚定父布局右端,作为固定锚点 --> <Button android:id="@+id/next_10_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@color/colorPrimary" android:onClick="next10Handler" android:text="Skip 10" android:minHeight="30dp" android:textColor="@android:color/background_light" android:layout_alignParentEnd="true" /> <Button android:id="@+id/next_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@color/colorPrimary" android:onClick="nextHandler" android:text="Skip" android:minHeight="30dp" android:textColor="@android:color/background_light" android:layout_toLeftOf="@id/next_10_button" /> <Button android:id="@+id/delete_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@color/colorPrimary" android:onClick="deleteHandler" android:text="Delete" android:minHeight="30dp" android:textColor="@android:color/background_light" android:layout_toLeftOf="@id/next_button" /> </RelativeLayout>
内容的提问来源于stack exchange,提问作者Yankang Meng
相关产品推荐
相关产品推荐

