【问题标题】:How to put Text View above the Image and at the bottom of Layout如何将文本视图放在图像上方和布局底部
【发布时间】:2016-04-11 11:54:33
【问题描述】:

我几乎尝试了从GravityalignParentToplayout_above 的所有方法,但这里似乎没有任何效果。

我有一个ImageView 和一个TextView 我正在使用ViewPager 来显示不同的图像及其各自的text

问题在于,它在顶部显示Text,在其后显示图像。

我希望它显示Image,并在其下方显示TextView

这是我添加以下xml后得到的结果

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

    <ImageView
        android:id="@+id/image_view"
        android:layout_width="match_parent"
        android:layout_height="200dp"

        />
    <TextView
        android:id="@+id/image_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:text="Image Text"
        />

</RelativeLayout>

请指导我如何将Text 放在Image 之后

【问题讨论】:

    标签: android xml android-layout android-studio


    【解决方案1】:

    试试这个对你有帮助

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="300sp"
        android:src="@drawable/wallpaper"
        android:scaleType="centerCrop"
        android:id="@+id/imageView" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="This is test"
        android:textColor="#000"
        android:textSize="20dp"
        android:gravity="center"
        android:layout_alignBottom="@+id/imageView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
    </RelativeLayout>
    

    【讨论】:

      【解决方案2】:

      使用android:layout_below 属性应该可以解决您的问题:

      <?xml version="1.0" encoding="utf-8"?>
      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical" android:layout_width="match_parent"
          android:layout_height="match_parent">
      
          <ImageView
              android:id="@+id/image_view"
              android:layout_width="match_parent"
              android:layout_height="200dp"
              />
      
          <TextView
              android:id="@+id/image_text"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="bottom"
              android:layout_below="@+id/image_view"
              android:text="Image Text"
              />
      
      </RelativeLayout>
      

      【讨论】:

      • Stackoverflow 处于最佳状态!!非常感谢:)
      【解决方案3】:

      在您的TextView 中添加android:layout_below="@+id/image_view"

      【讨论】:

        【解决方案4】:

        试试这个:

        <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                        android:orientation="vertical" android:layout_width="match_parent"
                        android:layout_height="match_parent">
        
            <ImageView
                    android:id="@+id/image_view"
                    android:layout_width="match_parent"
                    android:layout_alignParentBottom="true"
                    android:layout_height="200dp"
        
                    />
            <TextView
                    android:id="@+id/image_text"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_above="@+id/image_view"
                    android:paddingBottom="8dp"
                    android:text="Image Text"
                    />
        
        </RelativeLayout>
        

        希望对你有帮助!!

        【讨论】:

          【解决方案5】:

          你可以定义这种结构的方式,第一个定义 Textview 属性 android:layout_below 为 ImageView id,如下所示

          <?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:orientation="vertical">
          
              <ImageView
                  android:id="@+id/image_view"
                  android:layout_width="match_parent"
                  android:layout_height="200dp"
          
                  />
          
              <TextView
                  android:id="@+id/image_text"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_alignBottom="@+id/image_view"
                  android:layout_gravity="bottom"
                  android:text="Image Text" />
          
          </RelativeLayout>
          

          还有一个使用 LineLinearLayout 并定义垂直方向,如下所示

          <?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">
          
              <ImageView
                  android:id="@+id/image_view"
                  android:layout_width="match_parent"
                  android:layout_height="200dp"
          
                  />
          
              <TextView
                  android:id="@+id/image_text"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="Image Text" />
          
          </LinearLayout>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-01-05
            • 1970-01-01
            • 2019-01-07
            • 1970-01-01
            • 1970-01-01
            • 2014-04-29
            相关资源
            最近更新 更多