【问题标题】:Android: Placing ImageView on overlap between layoutsAndroid:将ImageView放在布局之间的重叠上
【发布时间】:2022-01-15 00:19:32
【问题描述】:

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

这在 XML 中可行吗?

我现在唯一的猜测是在实际代码中执行此操作。

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

【问题讨论】:

  • 是的,是可能的。使用布局开始
  • 什么是“布局开始”?你能把我链接到 Android 文档吗?
  • 你需要使用RelativeLayout。
  • 我想以边缘为中心,而不是从边缘开始,我仍然很困惑

标签: android android-layout android-xml


【解决方案1】:

这将满足您的需求,使用固定高度的图像或以编程方式计算。

<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>

【讨论】:

  • @paul 如何使用材料设计(CollapsingToolbarLayout 和 NestedScrollView)设计相同的布局
  • 不是通用的,应该用CoordinatorLayout
【解决方案2】:

试试这个方法,希望能帮助你解决问题。

<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>

【讨论】:

  • 感谢您的建议,但这不适用于不同尺寸的布局
【解决方案3】:

使用约束布局 然后假设有一个 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>

应该这样做

【讨论】:

    【解决方案4】:
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity2"
    android:orientation="vertical"
    >
    
    
    
    <RelativeLayout
        android:id="@+id/layouttop"
        android:layout_width="match_parent"
        android:layout_height="160dp"
        android:background="@drawable/dhaka"
        android:layout_alignParentTop="true"
        android:layout_marginTop="15dp"
        android:layout_marginRight="15dp"
        android:layout_marginLeft="15dp"
        >
    
    
    
    </RelativeLayout>
    
    
    
    <RelativeLayout
        android:id="@+id/layoutbutton"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        android:layout_marginRight="15dp"
        android:layout_marginLeft="15dp"
        android:layout_below="@+id/layouttop"
        >
    
    
    </RelativeLayout>
    
    
    
    
    <ImageView
        android:id="@+id/imagemiddle"
        android:layout_width="wrap_content"
        android:layout_height="150dp"
        android:src="@drawable/man"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="-50dp"
        android:layout_above="@+id/layoutbutton"
        />
    

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多