Android 将 TextView 保持在 ImageView 内的固定位置

标签 android android-layout textview imageview

我目前正在尝试获取某种动态信用卡图片,但我想要的是能够设置数字和信用卡,这是一个 ImageView 。现在我需要我的 TextView 在所有屏幕尺寸的相同位置,但我很难实现这一点。

谁能指出我正确的方向?或者与我分享更好的方法?这是我现在得到的:

<RelativeLayout 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:background="#fff"
            android:padding="10dp">

<ImageView
    android:id="@+id/creditcardimage"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    app:srcCompat="@drawable/creditcardblue"/>

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/creditcardimage"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginBottom="81dp"
    android:layout_marginLeft="30dp"
    android:layout_marginStart="30dp"
    android:text="1234 5678 9876 5432"
    android:textSize="20sp"/>

这是这个想法的图片:

CreditCard preview

谢谢!

最佳答案

您可以按照以下步骤操作

  1. 有一个固定大小的引用 View 。
  2. 在该 View 上绘制文本。由于 View 具有固定大小,因此将绘制文本 始终在正确的位置。
  3. 现在将引用 View 和重叠 TextView 转换为位图。
  4. 现在将此位图设置为实际 ImageView 。此 ImageView 与文本一起 将根据当前设备配置进行扩展。

您还可以设置 adjustViewBonds 和其他属性,以便 imageview 的宽高比不会改变。

关于Android 将 TextView 保持在 ImageView 内的固定位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43725187/

相关文章:

java - 已弃用 Httpclient

android - 使用线程移动图像

android - 从 fragment 类主 Activity 调用自定义 ListView 适配器

关于 CheckBox 和 RadioButton 的 Android xml 布局

android - 带有包装内容项的 RecyclerView

android - 使 ListView 内的 TextView 使用默认的 ListView 行样式

android - TextView 中的可点击单词

android - 如何在 Android 中设置动画缩放

没有点的Android单行TextView

java - HttpResponse 在 Android 和 Java 中不同