我学习了 Android XML,但遇到了布局方面的问题。 可以看到,我声明了2个线性布局和1个相对布局,所以顺序是线性(1)-相对(2)-线性(3) 然而,当我运行它时,显示屏显示线性 (1) - 线性 (3) - 相对 (2)。
所以,我想知道布局中是否有优先级或规则:)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffff0000"
>
<Button
android:id="@+id/backBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Linear_Layout1 (1)"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff000000"
>
<TextView
android:id="@+id/text01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Relative_layout1 (2)"
android:textColor="#ff0000ff"
android:textSize="19dp"
/>
<TextView
android:id="@+id/text02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mosquito"
android:textColor="#ff00ff00"
android:textSize="15dp"
android:background="#ffff0000"
android:layout_centerInParent="true"
android:layout_toRightOf="@id/text01"
/>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="vertical"
android:background="#fff0f0f0"
>
<TextView
android:id="@+id/text03"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Linear_layout2 (3)"
/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
最佳答案
No 没有任何优先级。
您已将 android:layout_centerInParent="true" 设置为 text01 并且相对布局的任何 subview 都没有对齐。
当您没有为相对布局的 subview 分配任何对齐方式时,就会出现重叠。(并从下到上添加到堆栈中)
查看更新的 View ,它在相对布局中从上到下添加 View
<Button
android:id="@+id/backBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Linear_Layout1 (1)" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff000000" >
<TextView
android:id="@+id/text01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Relative_layout1 (2)"
android:textColor="#ff0000ff"
android:textSize="19dp" />
<TextView
android:id="@+id/text02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/text01"
android:layout_toRightOf="@id/text01"
android:background="#ffff0000"
android:text="Mosquito"
android:textColor="#ff00ff00"
android:textSize="15dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/text02"
android:background="#fff0f0f0"
android:orientation="vertical" >
<TextView
android:id="@+id/text03"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Linear_layout2 (3)" />
</LinearLayout>
</RelativeLayout>
关于java - Android布局优先级?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25239315/