安卓系统。将ImageView放在布局之间的重叠处[英] Android: Placing ImageView on overlap between layouts

本文是小编为大家收集整理的关于安卓系统。将ImageView放在布局之间的重叠处的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在尝试在两个布局之间的重叠点上放置一个ImageView.在下面的图片中,我的目标是将ImageView放置在白色广场所在的位置. 注意:重叠点不一定垂直居中,如下所示

在此处输入图像说明

在XML中是可能的吗?

我唯一的猜测现在是在实际代码本身中这样做.

编辑8/3/2016:供参考,我认为ConstraintLayouts可能是这些类型问题的最佳未来解决方案 http://tools.android.com/tech-docs/layout-editor

推荐答案

这将执行您想要的,固定高度的图像,或以编程方式计算.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <RelativeLayout
        android:id="@+id/layoutTop"
        android:layout_width="match_parent"
        android:layout_height="200dp" >
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/layoutBottom"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_alignParentBottom="true"
        android:layout_below="@id/layoutTop" >
    </RelativeLayout>

    <ImageView
        android:id="@+id/overlapImage"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_above="@id/layoutBottom"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="-20dp" <!-- This should be always half the height, can also be calculated and added programtically -->
        android:adjustViewBounds="true"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>

其他推荐答案

尝试这种方式,希望这将有助于您解决您的问题.

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">

        </LinearLayout>
    </LinearLayout>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"
        android:adjustViewBounds="true"
        android:layout_gravity="center"/>
</FrameLayout>

其他推荐答案

使用约束布局 然后假设有一个top_view和bottom_view 您希望在Top_View和Bottom_View之间放置视图

<androidx.constraintlayout.widget.ConstraintLayout 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"
android:orientation="vertical">
<LinearLayout
    android:id="@+id/top_view"
    android:layout_width="match_parent"
    android:layout_height="@dimen/dp_size_100"
    android:background="@color/white"
    android:orientation="vertical"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<LinearLayout
    android:id="@+id/middle_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layout_constraintBottom_toBottomOf="@id/top_view"
    app:layout_constraintTop_toBottomOf="@id/top_view">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="abcd" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ipsum" />

</LinearLayout>

这应该会这样做

本文地址:https://www.itbaoku.cn/post/102522.html

问题描述

I am trying to place an ImageView on the overlap point between two layouts. In the picture below, my goal would be to place the ImageView where the white square is. NOTE: The overlap point will not necessarily be centered vertically as shown below

enter image description here

Is this possible in XML?

My only guess right now is to do this in the actual code itself.

EDIT 8/3/2016: For reference, I think ConstraintLayouts may be the best future solution for these types of problems http://tools.android.com/tech-docs/layout-editor

推荐答案

This will do what you want, with either an image of fixed height, or calculated programatically.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <RelativeLayout
        android:id="@+id/layoutTop"
        android:layout_width="match_parent"
        android:layout_height="200dp" >
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/layoutBottom"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_alignParentBottom="true"
        android:layout_below="@id/layoutTop" >
    </RelativeLayout>

    <ImageView
        android:id="@+id/overlapImage"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_above="@id/layoutBottom"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="-20dp" <!-- This should be always half the height, can also be calculated and added programtically -->
        android:adjustViewBounds="true"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>

其他推荐答案

Try this way,hope this will help you to solve your problem.

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">

        </LinearLayout>
    </LinearLayout>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"
        android:adjustViewBounds="true"
        android:layout_gravity="center"/>
</FrameLayout>

其他推荐答案

use constraint layout then assume there is a top_view and bottom_view you want to place the view between top_view and bottom_view

<androidx.constraintlayout.widget.ConstraintLayout 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"
android:orientation="vertical">
<LinearLayout
    android:id="@+id/top_view"
    android:layout_width="match_parent"
    android:layout_height="@dimen/dp_size_100"
    android:background="@color/white"
    android:orientation="vertical"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<LinearLayout
    android:id="@+id/middle_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layout_constraintBottom_toBottomOf="@id/top_view"
    app:layout_constraintTop_toBottomOf="@id/top_view">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="abcd" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ipsum" />

</LinearLayout>

This should do it