如何在android线性布局中对齐父底部?

时间:2013-12-17 11:02:33

标签: java android android-layout layout

我有一个linearlayout

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

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

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>

7 个答案:

答案 0 :(得分:17)

实际上,您可以将父元素的重力设置为底部

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

答案 1 :(得分:4)

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

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

    <SomeViewThatNeedsGoBottom
                             ... />

</LinearLayout>

答案 2 :(得分: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>

答案 3 :(得分: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>

答案 4 :(得分: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>

`

答案 5 :(得分:0)

使用:

android:gravity =“ bottom”

示例:

<LinearLayout
android:layout_weight="7"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"

android:weightSum="5">
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="some text"
    android:layout_weight="2"
    android:gravity="bottom"
   />
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="some text"
    android:layout_weight="1"/>
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="some text"android:gravity="center"
    android:layout_weight="2"/>

答案 6 :(得分:0)

在LinearLayout中放置相对 并将Relative设置为matchparent,因此您可以使用layoutalignparentbottom

<?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" />

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

</LinearLayout>
相关问题