2013-12-17 80 views
6

我有一个线性布局如何在android线性布局中对齐父级底部?

我想在它的底部创建一个切片。

我知道有一些选择,但我有点困惑

1)android:layout_gravity:"bottom" - >这并没有为我工作的某些原因。

2)android:gravity_weight="0"并给兄弟之前android:gravity_weight:"1"

3)android:height="wrap_content",给兄弟之前android:height:"match_parent"

我知道如何使用RelativeLayout的做到这一点,但我想练习的LinearLayout

你会建议什么?

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/blue_bg" 
    android:orientation="vertical" > 

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="10dp" 
     android:layout_marginTop="5dp" 
     android:src="@drawable/signup_illu_why" /> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="" 
     android:orientation="horizontal" /> 

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:src="@drawable/signup_skip_icon" /> 

</LinearLayout> 
+3

您是否尝试过使用RelativeLayout的?它可能更适合您的需求,并且RelativeLayout更加高效并且是一般推荐的方式。 – janos

+0

我知道如何做到这一点使用relativeLayout,但我想练习linearLayout –

+0

类似的 - 检查解决方案http://stackoverflow.com/a/31987803/3624307 – ashumeow

回答

1
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@android:color/black" 
    android:orientation="vertical" 
    android:weightSum="1"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:layout_weight="0.8" > 

     <ImageView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:src="@drawable/ic_launcher" /> 

     <ImageView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:src="@drawable/ic_launcher" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:layout_weight="0.2" /> 

</LinearLayout> 
+0

您不能将'android:orientation'添加到RelativeLayout。 –

+0

是的,请检查编辑答案。这是因为我直接改变布局,而不是手动 –

+0

我知道如何使用relativeLayout来做到这一点,但我想练习linearLayout –

1

试试这个

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="match_parent" 
     android:background="#ffffff" 
     android:orientation="vertical" > 

     <ImageView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 

      android:src="@drawable/ic_launcher" /> 
     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:orientation="vertical" > 

     <ImageView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:src="@drawable/ic_launcher" /> 

    </LinearLayout> 

    </RelativeLayout> 
14

其实,你可以在父元素的重心设置于底部

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/blue_bg" 
    android:orientation="vertical" 
    android:gravity="bottom" > 

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="10dp" 
     android:layout_marginTop="5dp" 
     android:src="@drawable/signup_illu_why" /> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="" 
     android:orientation="horizontal" /> 

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:src="@drawable/signup_skip_icon" /> 

</LinearLayout> 
+0

但后来一切都会在底部 – user25

+0

这是正确的答案 - http://stackoverflow.com/a/20633003/4548520我们应该使用'RelativeLayout'作为父母,并设置'android:layout_alignParentBottom =“true”'为孩子我们想要在底部 – user25

+0

@ user25问题没有指定哪个子视图应该底部对齐,所以我假设他希望每个子视图都位于底部 –

0

用的FrameLayout环绕你的线性布局,让重力作为底.. 其简单比你想象的...许多布局只是用于更容易移动设计

<FrameLayout 
android:layout_height="fill_parent" 
android:layout_width = "fill_parent"> 

<LinearLayout` 
android:layout_height="wrap_content" 
android:layout_width = "wrap_content" 
android:layout_gravity = "bottom" 
android:orientation = "vertical"> 

<Button 
android:layout_height="wrap_content" 
android:layout_width = "wrap_content" 
android:text = "btn"/> 

</LinearLayout> 

</FrameLayout> 

`

2
<LinearLayout 
    android:orientation="vertical" 
           ... > 

    <Space 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" /> 

    <SomeViewThatNeedsGoBottom 
          ... /> 

</LinearLayout> 
相关问题