【问题标题】:android layout_weight with RelativeLayout带有RelativeLayout的android layout_weight
【发布时间】: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


【解决方案1】:

只需在两个 RelativeLayout 中间添加一个 Textview,属性中心位于父级,top 将位于该 textView 上方,LinearLayout 下方将位于该 Textview 下方,您可以使该 textview 透明或您选择的任何内容(我与您提到的相同颜色)请参见下面的代码。

<?xml version="1.0" encoding="utf-8"?>
<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_above="@+id/tv_dummy"
    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:gravity="center"
            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:gravity="center"
            android:text="B status" />

    </LinearLayout>
</LinearLayout>

<TextView
    android:id="@+id/tv_dummy"
    android:layout_centerInParent="true"
    android:background="#5080ce"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<LinearLayout
    android:id="@+id/bottom_layout"
    android:layout_below="@+id/tv_dummy"
    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:gravity="center"
            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:gravity="center"
            android:text="D status" />
    </LinearLayout>
</LinearLayout>

【讨论】:

  • 谢谢!我将高度更改为 1dp,使其几乎不可见
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-16
  • 1970-01-01
  • 2021-09-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多