Android:alignment父级底部+底部边距

我已经使用了一个相对布局,我想在屏幕底部设置button,但是这一切都放在了底部,我想有一些保证金,所以它之间的屏幕/查看和button。 然而,无论我做button的边缘只是没有做任何事情2.1 +由于某种原因。 相对布局包含一个背景,所以我不能,但在边缘。

任何人都知道这个解决办法?

<?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:layout_marginBottom="0dp" android:background="@drawable/background" > <Button android:id="@+id/confirm_mobile_button_next" android:layout_width="fill_parent" android:layout_height="40dip" android:layout_alignParentBottom="true" android:layout_alignParentLeft="false" android:layout_centerHorizontal="false" android:layout_centerInParent="false" android:layout_margin="15dp" android:background="@drawable/button_shape_selector" android:paddingLeft="10dip" android:paddingRight="10dip" android:text="@string/confirm_mobile_no_continue" android:textColor="@color/white" android:textStyle="bold" /> </RelativeLayout> 

您可以简单地将填充添加到RelativeLayout而不是Button的边距,例如android:paddingBottom="15dp"

一般来说,我总是使用API​​ Level 8设置在Exclipse预览中testing我的布局。 这为大多数设备提供了相当准确的结果,包括ICS和JB。

你可以做的另一件事是把一个Viewalignment到RelativeLayout的底部,并将其高度设置为你想要使用的底部边距(或者简单地为layout_marginBottom指定一个值),如下所示:

  <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" > <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/some_image" android:adjustViewBounds="true" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Some Overlay" android:padding="3dip" android:gravity="center_vertical|center_horizontal" android:layout_above="@+id/empty_view" android:layout_marginBottom="35dip" /> <View android:id = "@+id/empty_view" android:layout_height = "30dip" android:layout_width = "match_parent" android:visibility="invisible" android:layout_alignParentBottom="true" /> </RelativeLayout> 

这个例子用ImageView填充RelativeLayout ,并在TextView放置一个TextView

Yu可以使用translateY属性

 translateY="-16dp" 

最终的代码

 <?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:layout_marginBottom="0dp" android:background="@drawable/background"> <Button android:id="@+id/confirm_mobile_button_next" android:layout_width="fill_parent" android:layout_height="40dip" android:layout_alignParentBottom="true" android:layout_alignParentLeft="false" android:layout_centerHorizontal="false" android:layout_centerInParent="false" android:layout_margin="15dp" android:background="@drawable/button_shape_selector" android:paddingLeft="10dip" android:paddingRight="10dip" android:text="@string/confirm_mobile_no_continue" android:textColor="@color/white" android:textStyle="bold" android:translateY="-16dp" /> </RelativeLayout> 

我认为最好的方法是在XML中设置android:layout_alignParentBottom,然后在java代码中:

 Button confirm_mobile_button_next = (Button)findViewById(R.id.confirm_mobile_button_next) 

 confirm_mobile_button_next.setTransitionY(-THE_VALUE) or setTransitionX(-THE_VALUE) 

由于我偶然发现了这个问题,看到没有答案适合我的情况,我开始思考半秒钟,并通过在RelativeLayout示例中的视图上设置负边距来解决问题:

 <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_width="48dp" android:layout_height="48dp" android:layout_marginTop="-8dp" android:layout_marginStart="-8dp"> </RelativeLayout> 

这应该certificate对某些人有用。

我认为最好的办法是设置:

 <... android:layout_alignParentBottom="true" /> 

那么在后面的java代码中:

 Button confirm_mobile_button_next = (Button)findViewById(R.id.confirm_mobile_button_next) confirm_mobile_button_next.setTransitionY(-THE_VALUE) or setTransitionX(-THE_VALUE) 

您可以使用ViewGroup(例如,FrameLayout或LinearLayout)来包装视图。 在ViewGroup外部设置alignParentBottom,然后marginBottom可以在View里面工作。

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <FrameLayout android:id="@+id/fl_layout" android:layout_alignParentBottom="true" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/tv_layout" android:text="hello world" android:layout_marginBottom="50dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </FrameLayout> </RelativeLayout> 

只有@franny zhaobuild议的解决scheme如下。

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <FrameLayout android:id="@+id/fl_layout" android:layout_alignParentBottom="true" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/tv_layout" android:text="hello world" android:layout_marginBottom="50dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </FrameLayout> </RelativeLayout> 

如果按照其他人的build议将填充添加到底部alignment的视图,则视图背景也将延伸。 如果你有彩色背景,那么视图看起来就像粘在底部。 填充和边距完全不同,填充是视图的一部分,但边距在视图之间留下空间。

试试这样:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="17dp" android:text="Button" /> 

这是另一种select。 如果要设置孩子的边距而不是父级填充,则应将android:layout_marginTop的值设置为所需边距的两倍,然后将android:layout_centerVertical设置为true。 上边距是给定值的两倍,以补偿底部边距。 这样你将在子视图周围有一个相等的顶部和底部边距。

 <Button android:id="@+id/confirm_mobile_button_next" android:layout_width="fill_parent" android:layout_height="40dip" android:layout_alignParentBottom="true" android:layout_alignParentLeft="false" android:layout_centerVertical="true" android:layout_centerInParent="false" android:layout_marginLeft="15dp" android:layout_marginRight="15dp" android:layout_marginTop="30dp" android:background="@drawable/button_shape_selector" android:paddingLeft="10dip" android:paddingRight="10dip" android:text="@string/confirm_mobile_no_continue" android:textColor="@color/white" android:textStyle="bold" /> 

它会给你相同的结果。