我想知道是否有方法在Android上的图像上覆盖文本并满足各种不同的屏幕尺寸?目前我只是使用图像作为布局的背景图像,然后我只是使用textview的。在Android上的图像上覆盖文本
我遇到的问题是,当图像在具有较小或较大屏幕的其他设备上缩放时,此文本的位置会发生变化,并且看起来不同,因为文本的缩放方式与图像不同设备,即使我使用DP来定位文本视图。
有什么方法可以将这些textview定位到屏幕上的某个点,以便它在不同的屏幕尺寸上看起来差不多?
我想知道是否有方法在Android上的图像上覆盖文本并满足各种不同的屏幕尺寸?目前我只是使用图像作为布局的背景图像,然后我只是使用textview的。在Android上的图像上覆盖文本
我遇到的问题是,当图像在具有较小或较大屏幕的其他设备上缩放时,此文本的位置会发生变化,并且看起来不同,因为文本的缩放方式与图像不同设备,即使我使用DP来定位文本视图。
有什么方法可以将这些textview定位到屏幕上的某个点,以便它在不同的屏幕尺寸上看起来差不多?
选中此项。这个Xml在任何设备上都能正常工作。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/yourimage"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true" />
<TextView
android:id="@+id/advrttext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Center Text in any Device"
android:textStyle="bold"
android:textSize="40sp"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="59dp" />
</RelativeLayout>
您也可以使用相对布局,做这样的事情
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="250dp"
android:layout_height="250dp"
android:background="@drawable/uninorsample"
android:id="@+id/imageView12"/>
<TextView
android:id="@+id/example_Txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Example Text"
android:textSize="16sp"
android:gravity="bottom|center_horizontal"
android:textColor="#FFFFFF"
android:textStyle="bold"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignBottom="@+id/imageView12"
android:layout_alignRight="@+id/imageView12"
android:layout_alignEnd="@+id/imageView12"/>
</RelativeLayout>
您可以设置图像在超过设定的TextView任何部件或布局的背景上,通过适当的dimension.ie即可在相对布局中设置阴影图像并在其上设置textview使用像centerinparent这样的属性,因此它将始终在任何DP中的父级中心。
试试这个: -
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/clock_number"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Hello World!" />
</RelativeLayout>
使用的FrameLayout
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorAccent"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Overlayed Text"
android:layout_gravity="center"/>
</FrameLayout>
只需设置图像作为背景为您的TextView。并正确对齐文本(顶部,底部,中心,...)。 –
如果我正确理解它,您希望您的文本大小与图像大小一致?看看这个:https://github.com/grantland/android-autofittextview你现在可以将这个文本视图对齐到你想要在你的图像视图上的位置,文本将按照图像视图比例缩放。 –
您也可以使用相对布局并执行此类操作 – Aziz