簡體   English   中英

如何在左上角的圖像上疊加文字

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

我想在 Imageview 的左上角的圖像上有 textview 類似於這個。 我怎樣才能做到這一點。 在此處輸入圖片說明

我有這個代碼,但它沒有做我想要的。

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

提前感謝您的幫助。

嘗試使用ConstraintLayout 這是一個簡單的例子。 對於分層,視圖按 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