【问题标题】:ANDROID -- How to display text on an image?ANDROID——如何在图像上显示文本?
【发布时间】:2011-02-16 16:59:45
【问题描述】:

好的......所以这是场景......从头开始,我想显示三颗星(我有一个我想使用的图像),居中。我想在每颗星上贴一个不同的词。当然,我不希望它硬编码,但是一旦我能正确布局,剩下的就很容易了。有人可以帮忙吗?我将不胜感激。

【问题讨论】:

  • 硬编码是什么意思?您想从 xml 文件中执行此操作吗?如果是这样,我会尝试使用相对布局来将图像视图与文本视图重叠并创建您的星形元素。否则,您可以创建一个自定义视图,该视图采用文本并自动将其显示在星形上方,并且您可以从代码中调用该元素

标签: android android-layout android-image


【解决方案1】:

如果您只想在图像上叠加文本,只需将 ImageView 包装在 FrameLayout 中,然后在 ImageView 之后定义一个 TextView。它将在顶部分层。对于您在最初问题中的建议,Reuben 和 Yahel 的答案将是更好的方法。

例子:

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/my_image"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Text on Top!"
        />
</FrameLayout>

【讨论】:

    【解决方案2】:

    只需使用每个视图(包括 TextView)所具有的“背景”属性来放置星标。工作完成。

    【讨论】:

    • 所以...如果我想在自定义图像中显示文本,我应该使用 TextView 吗?还是 ImageView 之类的?
    【解决方案3】:

    只需使用 TextViews 的 android:background 属性。

    【讨论】:

      猜你喜欢
      • 2013-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多