【问题标题】:Android: put element below another element and align at bottom of screenAndroid:将元素放在另一个元素下方并在屏幕底部对齐
【发布时间】:2014-07-23 01:20:48
【问题描述】:

我正在尝试定义一个布局,其中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>

【问题讨论】:

    标签: android layout alignment


    【解决方案1】:

    我会这样做:

    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

    中使用了元素的相对性

    [编辑]

    只是为了好玩...我优化了您的整个布局。
    请注意如何通过使用ma​​ny layoutless来实现相同的设计

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

    【讨论】:

    • 虽然没时间测试,但看起来不错。谢谢。下次写评论时尽量不要这么粗鲁。
    • 粗鲁吗?对此感到抱歉。我只是想让你专注于实际与最佳实践。毕竟,有时老师会大喊大叫。但他们只是想让你从错误中吸取教训。请珍惜它 - 尽可能简化,尽可能扁平化你的设计。并在不牺牲外观的情况下获得最佳的表演效果。 ;) - 您可能会注意到我必须保留一个内部 RelativeLayout,只是为了将这 2 个 TextView 水平居中(您可能会找到更好的替代方案来消除对该容器的需求)。
    • 刚刚试了代码,很完美。网格现在占据了所有可用空间,看起来好多了。顺便问一下,您知道我如何设置 GridView 以使项目均匀分布吗?现在,在设置android:stretchMode="spacingWidthUniform" 时,我最终在底部有很多空白。谢谢
    • 我很高兴你听从了我的建议,而且效果很好;)。也许这篇 stackoverflow.com/questions/7705330/… 的帖子回答了你的新问题?
    • 没用。我想我会尝试以编程方式设置项目大小
    【解决方案2】:

    通过将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>
    

    【讨论】:

    • 不好的做法是的,但很实用。我没有看到您建议的替代方法来解决它。
    • 我会告诉你一个更干净的选择。实用并不总是最好的选择。
    猜你喜欢
    • 2012-06-26
    • 1970-01-01
    • 2021-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多