相对布局不能嵌套吗?



[在此处输入图像描述][1]我在一个Relativelayout中放置了另一个Relative,但无法显示第二个Relative。不仅第二个Relativelayout的按钮无法显示,而且第二个相对论的背景也无法显示。

<RelativeLayout 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:id="@+id/global_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
tools:context=".Activities.Typing">
<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/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"
/>
<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_toRightOf="@id/delete_button"
android:layout_toLeftOf="@id/next_10_button"
/>
<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_toRightOf="@id/next_button" />
</RelativeLayout>
</RelativeLayout>

输出如下:[1]:https://i.stack.imgur.com/ZwnGx.jpg

尝试使用此

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/global_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true">
<LinearLayout
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/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"
/>
<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_toRightOf="@id/delete_button"
android:layout_toLeftOf="@id/next_10_button"
/>
<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_toRightOf="@id/next_button" />
</LinearLayout>
</LinearLayout>

尝试这样做:只需将第二个相对布局放在第一个相对布局中,并将delete按钮放在next_10_button的右侧。

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/functional_buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="250dp"
android:background="#DC143C">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/delete_button"
android:text="Delete"
android:layout_toRightOf="@id/next_10_button"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/next_button"
android:layout_toLeftOf="@id/next_button"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/next_10_button"
android:layout_toRightOf="@id/next_button"
/>

</RelativeLayout>
</RelativeLayout>

编写嵌套的RelativeLayout或任何类型的布局都不是一个好主意。试着用一层层次结构来解决这个问题。

最好的解决方案是使用ConstraintLayout。当前的ConstraintLayout足以创建最复杂的布局。所以,你也很有可能做到这一点。

试试

最新更新