【发布时间】:2016-12-01 12:02:17
【问题描述】:
我是 android 新手,遇到以下问题: 我想将我的屏幕拆分为 4 个线性布局,并且我需要根布局是相对布局, 我尝试使用 layout_weight 属性来在屏幕上平均分割我的 4 个布局,但是当我将根布局用作线性布局时,我才成功地做到了这一点。 布局xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/view_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:splitMotionEvents="false">
<LinearLayout
android:id="@+id/top_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<LinearLayout
android:id="@+id/a_status_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#5080ce">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="A status"/>
</LinearLayout>
<LinearLayout
android:id="@+id/b_status_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#356dc6">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="B status"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/bottom_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
>
<LinearLayout
android:id="@+id/c_status_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#325287">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="C status"/>
</LinearLayout>
<LinearLayout
android:id="@+id/d_status_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#26477c" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="D status"/>
</LinearLayout>
</LinearLayout>
(^我最后关闭了'RelativeLayout'标签,由于某种原因没有显示)
This is the screen when the root layout is LinearLayout
And this is when it's RelativeLayout(如上面的xml)
我可以使用“dp”单位将屏幕拆分为 4,但这样我遇到了其他问题...... 我的主要目标是能够将浮动图片从一个布局拖放到另一个布局,为此我需要使用 relativeLayout,另外我想知道图像被丢弃在哪个布局上,通过使用它给我的 Rect 属性出于某种原因的错误立场。
非常感谢! :)
【问题讨论】:
-
权重仅适用于 LinearLayout 容器。并且加权尺寸必须是0dp。
标签: android android-layout layout android-layout-weight