【问题标题】:Put elements between layouts [ANDROID]在布局之间放置元素 [ANDROID]
【发布时间】:2015-08-31 14:52:17
【问题描述】:

我正在做一个 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 谢谢

【问题讨论】:

标签: android layout


【解决方案1】:
<?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 中,然后添加了另一个带有“分隔符”的 LinearLayout。分隔线大小由权重精确设置,使其覆盖在中心的部分。

这是结果(如果您为“第二”布局设置了红色背景):

【讨论】:

  • 我很想知道为什么有人反对我的回答。不过,谢谢采纳。 :-)
【解决方案2】:

三个选项:

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

【讨论】:

    猜你喜欢
    • 2021-10-16
    • 2016-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多