【问题标题】:Android RelativeLayout: How to put a view above another view, without necessarily touching?Android RelativeLayout:如何将一个视图放在另一个视图之上,而不必触摸?
【发布时间】:2012-08-09 22:20:10
【问题描述】:

我正在显示一个可以包含任意行数的 ListView。我希望 ListView 包装内容。无论列表的大小如何,我还希望一些文本位于屏幕底部。像这样:

我的尝试如下,但是当内容很大时,它会导致它看起来像左下角的图像。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@color/blue">
    <TextView
        android:id="@+id/fine_print"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"  
        android:layout_below:"@+id/table"
        android:gravity="center_horizontal"
      />
    <ListView 
        android:id="@+id/table" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:background="@drawable/white_box">
    </ListView>
</RelativeLayout>

并添加

android:layout_above="@id/fine_print"

@id/table 使它看起来像右下角的图像。

我怎样才能让它做我想做的事?

【问题讨论】:

标签: android view alignment android-relativelayout


【解决方案1】:

答题:view.bringToFront();

你也可以在 xml 中试试这个:android:translationZ="10dp"

【讨论】:

    【解决方案2】:

    这看起来有点奇怪,但这是我如何让它工作的。由于底部的文本大小为“8dip”,并且根据屏幕方向占用 1 或 2 行,因此我在列表下方放置了“16dip”的填充。

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:background="@color/blue">
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/details_wrapper"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            android:paddingBottom="16dip">
            <ListView 
                android:id="@+id/table" 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content"
                android:background="@drawable/white_box">
            </ListView>
        </LinearLayout>
    
    <TextView
        android:id="@+id/fine_print"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"  
        android:gravity="center_horizontal"
      />
    </RelativeLayout>
    

    不过,仍然愿意接受有关如何使这个更清洁的建议。

    【讨论】:

    • 你有没有找到更清洁的解决方案?
    • @Kalina + yugidroid 解决方案有效。使用这个 xml 并添加 android:layout_below:"@+id/table" 将为您工作
    【解决方案3】:

    我认为您可以通过在您的 TextView 中添加类似的内容来解决您的问题

    android:layout_below:"@+id/table"

    如果有效,请告诉我。

    【讨论】:

    • 不,这也会导致底部的文本被推离屏幕,就像左下角的图像一样。
    • Kalina + @yugidroid 解决方案有效。使用这个 xml 并添加 android:layout_below:"@+id/table" 将为您工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-09
    • 2014-06-06
    • 2011-01-31
    • 1970-01-01
    相关资源
    最近更新 更多