【问题标题】:how to align parent bottom in android linear layout?如何在android线性布局中对齐父底部?
【发布时间】:2014-01-05 02:41:24
【问题描述】:

我有一个线性布局

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

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

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>

【问题讨论】:

  • 你试过使用RelativeLayout吗?它可能更适合您的需求,RelativeLayout 更高效,通常是推荐的方式。
  • 我知道如何使用relativeLayout,但我想练习linearLayout
  • 类似的-查看解决方案stackoverflow.com/a/31987803/3624307

标签: java android android-layout layout


【解决方案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="@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>

【讨论】:

  • 但是一切都会在底部
  • 这是正确的答案 - stackoverflow.com/a/20633003/4548520 我们应该使用 RelativeLayout 作为父级并为我们想要位于底部的子级设置 android:layout_alignParentBottom="true"
  • @user25 问题没有指定哪个子视图应该底部对齐,所以我假设他希望每个子视图都位于底部
【解决方案2】:
<LinearLayout
    android:orientation="vertical"
                              ... >

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

    <SomeViewThatNeedsGoBottom
                             ... />

</LinearLayout>

【讨论】:

    【解决方案3】:

    试试这个

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

      【讨论】:

      • 您不能将android:orientation 添加到RelativeLayout。
      • 是的,请检查编辑后的答案。那是因为我直接改变布局而不是手动
      • 我知道如何使用relativeLayout,但我想练习linearLayout
      • 检查我编辑的答案以使用线性布局。你可以用这两种布局来做,但如果你使用相对布局会更好,因为这样更容易使用。请相应地更改您的图像名称和颜色。
      【解决方案5】:

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

      `

      【讨论】:

        【解决方案6】:

        用途:

        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>
        

        【讨论】:

          【解决方案7】:

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

          【讨论】:

          • 为您的答案添加解释会很有帮助!
          • 虽然此代码可能会为问题提供解决方案,但最好添加有关其工作原理/方式的上下文。这可以帮助未来的用户学习并将这些知识应用到他们自己的代码中。在解释代码时,您也可能会以赞成票的形式从用户那里获得积极的反馈。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-10-07
          • 1970-01-01
          • 2016-11-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-03-27
          相关资源
          最近更新 更多