如何在Android约束布局中将图像重叠一半 [英] How to half overlap images in android constraint layout

查看:319
本文介绍了如何在Android约束布局中将图像重叠一半的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何方法可以仅使用约束布局将图像的一半放置在另一图像的顶部.我知道可以使用相对布局和框架布局来完成,但是在约束布局的情况下,仍然存在吗? 首选不需要任何高度/宽度硬编码的方式

Is there any way to place an images half is on top of another image using only constraint layout. I know it can be done using relative and frame layouts but in the case of constraint layout is there anyway? prefer ways which do not require any hardcoding of heights/widths

要求将如下所示

推荐答案

您可以仅使用约束布局来设置布局,如下所示:

you can set layout using only constraint layout like below :

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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">

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/holo_orange_dark"
        app:layout_constraintBottom_toTopOf="@id/guideline"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="0dp"  
        android:layout_height="0dp"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.5" />

    <ImageView
        android:id="@+id/imageView_upper"
        android:layout_width="70dp"
        android:layout_height="70dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@id/guideline"
        app:layout_constraintBottom_toBottomOf="@id/guideline"
        android:background="@android:color/holo_purple"/>

</android.support.constraint.ConstraintLayout>

注意:如果您使用的是androidx,则必须使用 androidx.constraintlayout.widget.ConstraintLayout 而不是 android.support.constraint.ConstraintLayout

Note: If you are using androidx then you have to use androidx.constraintlayout.widget.ConstraintLayout instead of android.support.constraint.ConstraintLayout

这篇关于如何在Android约束布局中将图像重叠一半的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆