Android:将元素放置在另一个元素下方并将其对齐到屏幕底部

23

我试图定义一个布局,其中一个ImageView位于屏幕底部并且在一个GridView下面,以避免重叠。

然而,这导致ImageView被设置在GridView下方,但不与屏幕底部对齐。

是否可以使用不同类型的布局来解决这个问题?

<?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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@drawable/background"
    android:orientation="vertical">

    <RelativeLayout 
        android:id="@+id/gs_top_text_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:paddingBottom="30dp">

        <RelativeLayout 
            android:id="@+id/gs_top_sub_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:paddingBottom="30dp">

            <TextView
                android:id="@+id/text_l"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"/>

            <TextView
                android:id="@+id/text_t"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"/>

        </RelativeLayout>

        <RelativeLayout 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/gs_top_sub_container"
            android:layout_centerHorizontal="true">

            <TextView
                android:id="@+id/text1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

            <TextView
                android:id="@+id/text2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/text1"/>

        </RelativeLayout>



    </RelativeLayout>



    <GridView
        android:id="@+id/gs_grid_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/gs_top_text_container"
        android:descendantFocusability="blocksDescendants"
        android:horizontalSpacing="2dp"
        android:verticalSpacing="2dp"
        android:numColumns="3"
        android:gravity="center"
        android:paddingBottom="10dp"
        android:paddingTop="10dp" />


    <ImageView
        android:id="@+id/gs_bottom_logo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/gs_grid_view"
        android:layout_alignParentBottom="true"
        android:adjustViewBounds="true"
        android:maxWidth="200dp"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"/>

</RelativeLayout>
2个回答

26

这是我如何做到的:

1- 将ImageView放在底部
2- 将GridView放在它上面。

<!-- First anchor this View to the bottom -->
<ImageView
    android:id="@+id/gs_bottom_logo"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:adjustViewBounds="true"
    android:maxWidth="200dp"
    android:paddingLeft="20dp"
    android:paddingRight="20dp"
/>
<!-- Then put this View ABOVE the ImageView -->
<GridView
    android:id="@+id/gs_grid_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/gs_top_text_container"
    android:layout_above="@id/gs_bottom_logo"
    android:descendantFocusability="blocksDescendants"
    android:horizontalSpacing="2dp"
    android:verticalSpacing="2dp"
    android:numColumns="3"
    android:gravity="center"
    android:paddingBottom="10dp"
    android:paddingTop="10dp"
/>

我使用了 RelativeLayout 中元素间的 相对性

[编辑]

只是为了好玩... 我优化了你的整个布局。
请注意,通过使用 更少的布局,实现了相同的设计

<?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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@drawable/background"
    >
    <TextView
        android:id="@+id/text_l"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:paddingBottom="30dp"
    />
    <TextView
        android:id="@+id/text_t"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:paddingBottom="30dp"
    />
    <!--
        This container is (unfortunately) still needed, to make these two
        textViews horizontally centered... :(
    -->
    <RelativeLayout
        android:id="@+id/rlCentering"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/text_l"
        android:layout_centerHorizontal="true"
        android:paddingBottom="30dp"
        >
        <TextView
            android:id="@+id/text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
        />
        <TextView
            android:id="@+id/text2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/text1"
        />
    </RelativeLayout>

    <!-- First, anchor this View to the bottom -->
    <ImageView
        android:id="@+id/gs_bottom_logo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:adjustViewBounds="true"
        android:maxWidth="200dp"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
    />
    <!-- Then put this View ABOVE the ImageView -->
    <GridView
        android:id="@+id/gs_grid_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/rlCentering"
        android:layout_above="@id/gs_bottom_logo"
        android:descendantFocusability="blocksDescendants"
        android:horizontalSpacing="2dp"
        android:verticalSpacing="2dp"
        android:numColumns="3"
        android:gravity="center"
        android:paddingBottom="10dp"
        android:paddingTop="10dp"
    />
</RelativeLayout>

这是我在图形编辑器中得到的内容(我放了一些图片和文本来标识TextView):

输入图像描述


虽然我没有时间测试它,但看起来不错。谢谢。下次写评论时尽量不要这么粗鲁。 - liarspocker
2
很抱歉,我是不是说话有些粗鲁了?其实我只是想让你注重实践而非最佳实践。毕竟,有时候老师会大声喊叫,但他们只是为了让你从错误中学习。请尽量简化你的设计,使其趋于扁平化,并在不牺牲外观的情况下获得最佳性能。 ;) - 你可能会注意到,我必须保留一个内部RelativeLayout,只是为了让这两个TextView水平居中(也许你可以找到更好的替代方法来消除需要该容器的需求)。 - Phantômaxx
刚试了一下代码,完美无缺。现在网格占用了所有可用的空间,看起来好多了。顺便问一下,你知道我怎么设置GridView使得项目均匀分布吗?目前,当设置android:stretchMode="spacingWidthUniform"时,底部会出现很多空白。谢谢。 - liarspocker
很高兴你听从了我的建议,并且它完美地奏效了;)。也许这篇帖子https://dev59.com/oWsz5IYBdhLWcg3wuKQm可以回答你的新问题? - Phantômaxx
没起作用。我想我会尝试通过编程设置项目大小。 - liarspocker
显示剩余2条评论

3
通过将 ImageView放在RelativeLayout中,设置RelativeLayout中的 layout_below 项和ImageView的 alignParentBottom 项来解决它:
<RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/gs_grid_view">
<ImageView
    android:id="@+id/gs_bottom_logo"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:adjustViewBounds="true"
    android:maxWidth="200dp"
    android:paddingLeft="20dp"
    android:paddingRight="20dp"/>
</RelativeLayout>

不好的实践确实存在,但它却很实用。我没有看到你提出的替代方法来解决这个问题。 - liarspocker
我会给你展示一种更简洁的选择。实用并不总是最好的选择。 - Phantômaxx

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接