【问题标题】:How to Overlay text over image at left top corner如何在左上角的图像上叠加文字
【发布时间】:2020-04-01 03:22:09
【问题描述】:

我想在与此类似的 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>

提前感谢您的帮助。

【问题讨论】:

  • 另外,为了让你下次问的问题更好,你应该说出代码在做什么以及你期望它做什么,而不是仅仅说“它没有做我想要的”。

标签: java android xml android-layout


【解决方案1】:

尝试使用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>

【讨论】:

    猜你喜欢
    • 2013-06-20
    • 2021-04-08
    • 1970-01-01
    • 2018-10-16
    • 1970-01-01
    • 1970-01-01
    • 2016-01-09
    • 1970-01-01
    • 2013-11-06
    相关资源
    最近更新 更多