簡體   English   中英

如何在android線性布局中對齊父底部?

[英]how to align parent bottom in android linear layout?

我有一個線性布局

我想在它的底部創建一個切片。

我知道有一些選擇,但我有點困惑

1) android:layout_gravity:"bottom" --> 由於某種原因,這對我不起作用。

2) android:gravity_weight="0"並在它之前給兄弟android:gravity_weight:"1"

3) android:height="wrap_content"並在它之前給兄弟android:height:"match_parent"

我知道如何使用相對布局來做到這一點,但我想練習線性布局

你有什么建議?

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

實際上,您可以將父元素的重力設置為底部

<?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>
<LinearLayout
    android:orientation="vertical"
                              ... >

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

    <SomeViewThatNeedsGoBottom
                             ... />

</LinearLayout>

試試這個

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

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

`

采用:

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM