【问题标题】:How to get right TextView position over Image for all screen types?如何为所有屏幕类型在 Image 上获得正确的 TextView 位置?
【发布时间】:2014-11-12 08:29:27
【问题描述】:

有一个带有文本区域的图像,上面有几个 TextView。我正在使用不同 dpi 的布局(layout-mdpi、layout-hdpi、layout-xhdpi),但对于不同的分辨率,我的 TextViews 仍然会移出。有没有办法为所有屏幕分辨率和 dpi 实现正确的 TextViews 位置? 使用“layout-hdpi-800x480”格式可以得到正确的结果,但我需要涵盖所有屏幕类型。

<?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"
android:background="#000000" >

<ImageView
    android:id="@+id/faqImage1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:scaleType="fitCenter"
    android:src="@drawable/image_01" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="180dp"
    android:layout_height="30dp"
    android:layout_alignLeft="@+id/faqImage1"
    android:layout_alignTop="@+id/faqImage1"
    android:layout_marginLeft="34dp"
    android:layout_marginTop="40dp"
    android:gravity="center"
    android:text="@string/text1" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="170dp"
    android:layout_height="40dp"
    android:layout_alignRight="@+id/faqImage1"
    android:layout_below="@+id/textView1"
    android:layout_marginRight="36dp"
    android:layout_marginTop="13dp"
    android:gravity="center"
    android:text="@string/text2" />

</RelativeLayout>

【问题讨论】:

  • 参见 ImageView.getImageMatrix() 和 Matrix.mapPoints()
  • 用FrameLayout把TextView放在ImageView上不是更好吗?

标签: android android-layout


【解决方案1】:

我认为这将对您有所帮助,他们的“android:layout_alignParentRight”和“android:layout_alignParentLeft”标签可用于相对布局子级,可帮助您在屏幕右侧显示文本视图

    <?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"
                android:background="#000000">

    <ImageView
            android:id="@+id/faqImage1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:scaleType="fitCenter"
            android:src="@drawable/image_default_logo"/>

    <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
             android:paddingLeft="30dp" 
             android:paddingTop="30dp"
            android:layout_alignTop="@+id/faqImage1"
            android:textSize="28sp"
            android:text="Text1"/>

    <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:paddingRight="30dp" 
             android:paddingBottom="30dp"
            android:textSize="28sp"
            android:gravity="center"
            android:text="Text1"/>

</RelativeLayout>

如果您想要其他东西,那么请放置您的设计屏幕截图,以实现您真正想要实现的目标。

【讨论】:

  • android:layout_alignParentRight 和 android:layout_alignParentLeft 没有给出正确的结果。这是带有方案link的图片链接。
  • 谁说你 android:layout_alignParentRight 和 android:layout_alignParentLeft 没有给出正确的结果。让我们检查最新更改的代码。如果你在 values 文件夹下的不同维度文件中定义它会更好,这将支持你适合不同的屏幕尺寸。
猜你喜欢
  • 2014-08-07
  • 1970-01-01
  • 2013-12-19
  • 1970-01-01
  • 1970-01-01
  • 2022-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多