简体   繁体   English

如何在左上角的图像上叠加文字

[英]How to Overlay text over image at left top corner

I want to have textview over image at top left corner of the Imageview similar to this.我想在 Imageview 的左上角的图像上有 textview 类似于这个。 how can I do this.我怎样才能做到这一点。 在此处输入图片说明

I have this code but It doesn't do what i want.我有这个代码,但它没有做我想要的。

 <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/myImageView" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/myImageSouce" android:scaleType="centerCrop"/> <TextView android:id="@+id/myImageViewText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="@dimen/spacing_medium" android:layout_gravity="center" tools:text="Test"/> </FrameLayout>

Thanks for help in advance.提前感谢您的帮助。

Try using a ConstraintLayout .尝试使用ConstraintLayout Here's a simple example.这是一个简单的例子。 For layering, views are layered in the order listed in the xml so if you want the TextView in front put it last in the xml对于分层,视图按 xml 中列出的顺序分层,因此如果您希望TextView在前面,请将其放在 xml 的最后

<?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"
    tools:context=".MainActivity">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="16dp"
        app:srcCompat="@drawable/ic_home_black_24dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Some Words"
        android:padding="20dp"
        android:background="#00ff00"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

Android Studio 预览版

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM