Android:将元素放在另一个元素下方并在屏幕底部对齐
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24002753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: put element below another element and align at bottom of screen
提问by liarspocker
I am trying to define a layout where an ImageViewis aligned at the bottom of the screen and also under a GridView, to avoid overlapping.
我试图定义一个布局,其中 anImageView在屏幕底部对齐,也在 a 下对齐GridView,以避免重叠。
This however results in the ImageViewbeing set just under the GridViewbut not aligned with the bottom of the screen.
然而,这会导致ImageView设置GridView在屏幕下方但未与屏幕底部对齐。
Can this be solved using a different kind of Layout?
这可以使用不同类型的布局来解决吗?
<?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>
回答by Phant?maxx
This is how I would do that:
这就是我将如何做到的:
1 - Put the ImageView on the Bottom
2 - Put the GridView ABOVE it.
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"
/>
This way I used the relativityof elements in a RelativeLayout
这样,我用相对论元素的一个RelativeLayout的
[EDIT]
[编辑]
Just for fun... I optimized your WHOLE LAYOUT.
Please note how the same designis achieved by using many layouts less.
只是为了好玩……我优化了你的整体布局。
请注意如何通过减少使用许多布局来实现相同的设计。
<?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>
And this is what I got in the Graphical Editor (I put some images I had and some texts to identify the TextViews):
这就是我在图形编辑器中得到的(我放了一些我拥有的图像和一些文本来识别 TextViews):


回答by liarspocker
Solved it by putting the ImageView in a RelativeLayout, setting the layout_belowitem in the RelativeLayout and the alignParentBottomitem of ImageView:
通过将ImageView 放在 RelativeLayout 中、在 RelativeLayout中设置layout_below项和alignParentBottomImageView 项来解决它:
<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>

