安卓:在布局重叠处放置ImageView

16

我想在两个布局的重叠点放置一个ImageView。 在下面的图片中,我的目标是将ImageView放置在白色正方形所在的位置。注意:重叠点不一定像下面显示的那样在垂直方向上居中

enter image description here

这在XML中可能吗?

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

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


是的,这是可能的。使用布局开始。 - Chefes
什么是“布局开始”?你能给我提供 Android 文档的链接吗? - Daiwik Daarun
http://developer.android.com/reference/android/widget/RelativeLayout.html#ALIGN_START - Chefes
你需要使用RelativeLayout。 - Chefes
我想要以边缘为中心,而不是从边缘开始,我仍然感到困惑。 - Daiwik Daarun
是的,这是可能的。 - Dharmendra Mishra
4个回答

37

这将根据固定高度或程序计算来实现您想要的效果。

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

1
@paul 如何使用Material Design(CollapsingToolbarLayout和NestedScrollView)设计相同的布局? - Rajesh
它不是通用的,应该使用CoordinatorLayout - Vasile Doe

0
<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"
    />

你的回答可以通过提供更多支持信息来改进。请编辑以添加进一步的细节,例如引用或文档,以便他人可以确认你的答案是正确的。您可以在帮助中心找到有关如何编写良好答案的更多信息。 - Community

0

尝试这种方式,希望能帮助您解决问题。

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

谢谢您的建议,但是对于不同尺寸的布局来说这并不可行。 - Daiwik Daarun

0

使用约束布局 假设有一个顶部视图和底部视图 你想在它们之间放置视图

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

这应该可以做到


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接