我目前正在尝试获取某种动态信用卡图片,但我想要的是能够设置数字和信用卡,这是一个 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"/>
这是这个想法的图片:
谢谢!
最佳答案
您可以按照以下步骤操作
- 有一个固定大小的引用 View 。
- 在该 View 上绘制文本。由于 View 具有固定大小,因此将绘制文本 始终在正确的位置。
- 现在将引用 View 和重叠 TextView 转换为位图。
- 现在将此位图设置为实际 ImageView 。此 ImageView 与文本一起 将根据当前设备配置进行扩展。
您还可以设置 adjustViewBonds 和其他属性,以便 imageview 的宽高比不会改变。
关于Android 将 TextView 保持在 ImageView 内的固定位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43725187/