【问题标题】:TextView above Image view in a layout布局中的图像视图上方的 TextView
【发布时间】:2012-03-02 20:16:24
【问题描述】:

我正在为我的屏幕使用这个布局:

    <?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" android:background="@drawable/tmp" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/uph" 
        android:layout_gravity="top|center" android:layout_marginTop="-10dp"/>

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/propfile" 
        android:layout_gravity="top|center"/>    
</LinearLayout>

我想在 imageView2 上方的屏幕上添加一个 TextView。

当我将它添加到 xml 时,它会显示在图像下方。 我需要做什么才能将TextView 放在图像上。

【问题讨论】:

    标签: java android android-layout


    【解决方案1】:

    试试这个:

        <?xml version="1.0" encoding="utf-8"?> 
            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" android:background="@drawable/tmp" >
    
    
            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:src="@drawable/uph" 
                 android:layout_marginTop="-10dp"/>
    
            <RelativeLayout  android:layout_width="match_parent"
                android:layout_height="wrap_content" 
                android:layout_gravity="top|center">
    
                <ImageView
                    android:id="@+id/imageView2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:src="@drawable/propfile" 
                />
    
               <TextView  android:layout_width="wrap_content"
                   android:layout_height="wrap_content" 
                   android:text="text" 
                   android:layout_centerInParent="true"
               /> 
          </RelativeLayout>   
        </LinearLayout>
    

    【讨论】:

      【解决方案2】:

      您可以在第二张图片所在的位置添加另一个 LinearLayout,例如:

      <?xml version="1.0" encoding="utf-8"?> 
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical" android:background="@drawable/tmp" >
      
          <ImageView
              android:id="@+id/imageView1"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:src="@drawable/uph" 
              android:layout_gravity="top|center" android:layout_marginTop="-10dp"/>
      
          <LinearLayout
              android:layout_width="wrap_content"
              android:layout_height="wrap_content">
              <TextView 
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="Text Goes Here" />
              <ImageView
                  android:id="@+id/imageView2"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:src="@drawable/propfile" 
                  android:layout_gravity="top|center"/>    
          </LinearLayout>
      </LinearLayout>
      

      【讨论】:

        【解决方案3】:

        这是另一种选择, 在您的 xml 文件中,

         <TextView android:layout_width="wrap_content"
                   android:layout_height="wrap_content" 
                   android:text="text" 
                   android:layout_centerInParent="true"
                   android:layout_above="idofyourImage"
               /> 
        

        希望对你有帮助

        【讨论】:

        • android:layout_above 在 LinearLayout 中无效。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-23
        • 1970-01-01
        • 2022-01-20
        • 1970-01-01
        相关资源
        最近更新 更多