【问题标题】:Positioning Android views on top of a bitmap将 Android 视图定位在位图之上
【发布时间】:2014-07-07 20:18:31
【问题描述】:

我有一个带有可绘制位图的 ImageView。每种设备密度(hdpi、xhdpi、xxhdpi 等)都有不同版本的位图。

我想在 ImageView 的顶部放置一个 TextView,并且相对于 ImageView 的位置。例如右侧 30 像素,顶部 50 像素。 TextView 需要始终显示在图像的某个部分,因此针对不同屏幕密度创建的每个版本的图像,其位置会有所不同。

像素定位似乎有效。我能够为不同版本的图像(hdpi、xhdpi 等)创建具有像素值的不同 XML 布局文件。但是,我无法在 dimen.xml 中指定 px 单位

这里用像素定位是正确的方法吗?使用相对于图像的像素进行定位是否适用于不同的设备?

【问题讨论】:

标签: android android-layout android-ui


【解决方案1】:

我会这样做。在 ImageView 上方放置一个 RelativeLayout 并将其颜色设置为 `#66cccccc。并在里面放一个TextView

<RelativeLayout 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/imageView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="fitXY"
        android:src="@drawable/yourImage" />

 <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="25dp"
        android:background="#66cccccc">

        <TextView
         android:id="@+id/textView1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:textColor="#fff"
         android:text="Test Description" />

    </RelativeLayout>


</RelativeLayout>

【讨论】:

  • 谢谢。我实际上使用 FrameLayout 进行 z 排序。然后我通过以像素为单位设置边距来定位 TextView。 TextView 不能相对定位,它需要相对于下面的 Image 使用像素进行绝对定位。这仍然适用于不同的设备吗?
  • 其实我从来没有用过FrameLayout,我也不知道你的问题。但我认为您可以创建两个不同的模拟器来查看差异。 (感谢您的支持)。
猜你喜欢
  • 1970-01-01
  • 2011-07-29
  • 2018-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-07
相关资源
最近更新 更多