【问题标题】:Place view on top and between two views将视图放在顶部和两个视图之间
【发布时间】:2013-05-13 01:18:45
【问题描述】:

想办法解决这个小问题。

下方左侧是我尝试为应用实现的视图示例。右侧是我最终得到的视图。

在我的 xml 模型中,我决定使用 RelativeLayout,但我无法让 TextView 在顶部和底部视图之间居中。

作为参考,这里是我的 xml 代码的表示:

 <RelativeLayout 
    android:layout_width:"match_parent"
    android:layout_height:"wrap_content">
    <ImageView
        android:id="@+id/BlackImage"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:scaleType="centerCrop"
        android:layout_alightParentTop="true"
        android:layout_centerHorizontal="true"/>
    <ImageView
        android:id="@+id/RedImage"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:scaleType="centerCrop"
        android:layout_below="@+id/BlackImage"
        android:layout_centerHorizontal="true"/>
    <TextView
        android:id="@+id/TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/BlackImage"
        android:layout_centerHorizontal="true"/>
 </RelativeLayout>

知道我缺少什么,或者如何改变以获得我想要的吗?

【问题讨论】:

  • 我想我会尝试使用FrameLayout,因为RelativeLayout 更适合用于沿边缘对齐的视图。无论如何,您可以尝试负边距或填充。
  • 试试下面的链接希望对你有帮助:- stackoverflow.com/questions/9539772/…> stackoverflow.com/questions/15976292/…> stackoverflow.com/questions/10195557/…>

标签: android android-layout android-relativelayout android-view


【解决方案1】:

试试下面的链接,希望对你有帮助:-

TextView above Image view in a layout

Android listview item display text over image using array adapter

how to put text under image view

 <?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>

【讨论】:

  • 不知道我可以为layout_gravity 提供多个选项。你每天学习新的东西!试试这个,谢谢你的链接!