Android:将元素放在另一个元素下面,并在屏幕底部对齐
我试图定义一个布局,其中Android:将元素放在另一个元素下面,并在屏幕底部对齐,android,layout,alignment,Android,Layout,Alignment,我试图定义一个布局,其中ImageView在屏幕底部对齐,也在GridView下对齐,以避免重叠 但是,这会导致将ImageView设置在GridView的正下方,但不会与屏幕底部对齐 使用不同的布局可以解决这个问题吗 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layou
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>
通过将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>
我会这样做:
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>
这是我在图形编辑器中得到的结果(我放了一些图片和文本来识别文本视图):
坏习惯是的,但很实用。我看不出你提出的解决办法。我会给你看一个更干净的办法。实用并不总是最好的选择。虽然我没有时间测试,但看起来不错。谢谢下次写评论的时候尽量不要这么粗鲁。是不是很粗鲁?对不起。我只是想让你专注于实践与最佳实践。毕竟,有时老师会大喊大叫。但他们只是想让你从错误中吸取教训。请珍惜这一点——尽可能简化,尽可能扁平化你的设计。并在不牺牲外观的情况下获得最佳的表演效果你可能会注意到我必须保持一个内部RelativeLayout,只是将这两个文本视图水平居中(你可能会找到一个更好的替代方案来消除对该容器的需要)。网格现在占据了所有可用空间,看起来好多了。顺便问一下,您知道如何设置GridView以使项目均匀分布吗?现在,我在设置android:stretchMode=“spacingWidthUniform”
时,在底部有很多空白。谢谢我很高兴你听从了我的建议,而且效果很好。也许这篇文章回答了你的新问题?没用。我想我会尝试按程序设置项目大小