在布局之间放置元素[ANDROID]

时间:2015-08-31 14:52:17

标签: android layout

我在Android中正在做一个用户界面。 我的想法是将这个界面划分为三个相等的部分(这很简单,三个布局有重量),但我想把图像放在这些布局的交互中。 例如,一个图像覆盖了layout1和layout2中的一些空格,并且在2和3中相同。

怎么办呢?我的代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:weightSum="1.0">


    <LinearLayout
        android:id="@+id/first"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.33">

    </LinearLayout>

    <LinearLayout
        android:id="@+id/second"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.33"></LinearLayout>

    <LinearLayout
        android:id="@+id/third"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.33"></LinearLayout>

</LinearLayout>

我的想法:Android: Placing ImageView on overlap between layouts 感谢

2 个答案:

答案 0 :(得分:0)

三个选项:

  1. 使视图介于两者之间。一世。即你的每一个 LinearLayouts的权重为0.30,您之间的视图得到a 重量为0.05
  2. 将视图放在LinearLayout 2和3的顶部(不完全相同 然后他们每个人的身高)
  3. 以编程方式执行,在代码中设置每个文件的高度。

答案 1 :(得分:0)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:weightSum="6.0" >

        <LinearLayout
            android:id="@+id/first"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="2"
            android:orientation="vertical" >
        </LinearLayout>

        <LinearLayout
            android:id="@+id/second"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="2"
            android:orientation="vertical" >
        </LinearLayout>

        <LinearLayout
            android:id="@+id/third"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="2"
            android:orientation="vertical" >
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:weightSum="6.0" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1.0"
            android:orientation="vertical" >
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/firstDividerLayout"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="2.0" >

            <ImageView
                android:id="@+id/firstDividerImageView"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_centerInParent="true"
                android:src="@drawable/ic_launcher" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/secondDividerLayout"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="2.0" >

            <ImageView
                android:id="@+id/secondDividerImageView"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_centerInParent="true"
                android:src="@drawable/ic_launcher" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1.0" >
        </RelativeLayout>
    </LinearLayout>

</RelativeLayout>

请注意,我将原始的LinearLayout包装在RelativeLayout中,然后我添加了另一个带有&#39;分隔符的LinearLayout。分隔符大小由权重精确设置,以便它覆盖在中心的部分。

这是结果(如果您为&#39;第二个&#39;布局设置了红色背景): enter image description here