【问题标题】:how to set TextView on ImageView in Android如何在 Android 中的 ImageView 上设置 TextView
【发布时间】:2013-11-09 22:41:08
【问题描述】:

我正处于 android 的学习阶段。我想在 ImageView 上设置 TextView。我不明白。我会很感激的。这是我的 XML 代码。

<LinearLayout 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="wrap_content"
        android:layout_height="380dp"       
        android:adjustViewBounds="true"    
        android:scaleType="fitCenter"
        android:background="@drawable/wf" />


    <TextView
   // what should i add here so that it can be visible on image view//
     />

</LinearLayout>

【问题讨论】:

  • 你的意思是你想把一个TextView放在一个ImageView的fromt中吗?您不能简单地使用 LinearLayout 来做到这一点,因为这会将所有视图并排排列。
  • 这没有意义。视图不能既是文本视图又是图像视图。如果要合并视图,可以使用视图组。
  • 不在图像视图前面,而我想在 ImageView 上设置 TextView...?
  • 你能说得更具体点吗?
  • 我通过 ImageView 添加了一张图片。现在我想在同一张图片上添加一些随机更改的文字...如何在所述图片上添加/设置 TextView....??希望你明白了

标签: android


【解决方案1】:

感谢大家,终于找到了解决方案。我使用 FrameLayout 来处理它。

这里是代码

<FrameLayout 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="wrap_content"
        android:layout_height="380dp"       
        android:adjustViewBounds="true"    
        android:scaleType="fitCenter"
        android:background="@drawable/wf" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="8"
        android:textSize="100sp"
        android:layout_marginTop="120dp"
        android:layout_marginLeft="160dp"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</FrameLayout>

【讨论】:

    【解决方案2】:

    我认为你想要的是像照片标题这样的东西,因此你可以实现的最佳方法是使用相对布局,你的 .xml 布局看起来像:

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="69dp"/>
    
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView1"
        android:layout_centerHorizontal="true"
        android:text="TextView" />
    

    注意textView的属性

    android:layout_below="@+id/imageView1"
    

    这是关键。 如果出于任何原因您更喜欢线性布局作为主布局,您可以将相对布局嵌套在嵌套在主线性布局中的表格行(或其他线性布局)中

    我希望这并不复杂

    PS:这是一个初学者的建议

    【讨论】:

      【解决方案3】:

      您还可以使用RelativeLayout 作为父元素,ImageView 作为第一个子元素,TextView 作为第二个子元素。

      【讨论】:

        【解决方案4】:

        你可以像这样使用RelativeLayout:

            <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="wrap_content"
                android:layout_height="380dp"       
                android:adjustViewBounds="true"    
                android:scaleType="fitCenter"
                android:background="@drawable/wf" />
        
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
               />
        
        </RelativeLayout>
        

        【讨论】:

          猜你喜欢
          • 2016-02-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-01-19
          相关资源
          最近更新 更多