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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20632407/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:14:30  来源:igfitidea点击:

how to align parent bottom in android linear layout?

javaandroidandroid-layoutlayout

提问by Elad Benda

I have a linearlayout

我有一个线性布局

and I want to create at the bottom of it a slice.

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

I know there are some options, but I'm a bit confused

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

1) android:layout_gravity:"bottom"--> this doesn't work for me for some reason.

1) android:layout_gravity:"bottom"--> 出于某种原因,这对我不起作用。

2) android:gravity_weight="0"and give the sibling before it android:gravity_weight:"1"

2)android:gravity_weight="0"并在它之前给兄弟姐妹android:gravity_weight:"1"

3) android:height="wrap_content"and give the sibling before it android:height:"match_parent"

3)android:height="wrap_content"并在它之前给兄弟姐妹android:height:"match_parent"

I know how to do this using relativeLayout, but I want to practice linearLayout

我知道如何使用相对布局来做到这一点,但我想练习线性布局

what would you suggest?

你有什么建议?

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

回答by Vaibhav Agarwal

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

回答by The Ray of Hope

try this

尝试这个

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

回答by Rick Royd Aban

Actually, you can set the parent element's gravity to bottom

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

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

回答by A_rmas

surround your linear layout with framelayout and give gravity as bottom.. its simple than you think... many layouts are used just for easier move towards design

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

`

`

回答by Andrey

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

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

    <SomeViewThatNeedsGoBottom
                             ... />

</LinearLayout>

回答by sanjeeb

Use:

用:

android:gravity="bottom"

example:

例子:

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

回答by gdrive my

Put Relative in LinearLayout and set Relative to matchparent, so you can use the layoutalignparentbottom

将Relative放在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>