【问题标题】:Put buttons at bottom of screen with LinearLayout?使用LinearLayout将按钮放在屏幕底部?
【发布时间】:2013-01-24 15:20:17
【问题描述】:

我有下面的代码,如何让3个按钮在底部?

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="60dp"
        android:gravity="center"
        android:text="@string/observer"
        android:textAppearance="?android:attr/textAppearanceLarge"
        tools:context=".asdf"
        android:weight="1" />

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

        <Button
            android:id="@+id/button1"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="145dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal|center"
            android:text="1" />

        <Button
            android:id="@+id/button2"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="145dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal|center"
            android:text="2" />

        <Button
            android:id="@+id/button3"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="145dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal|center"
            android:text="3" />
    </LinearLayout>

【问题讨论】:

  • 这个视图包含什么?框架布局?相对布局?
  • 您的代码中有错字。 android:weight="1" 可能是指android:layout_weight="1"。不过这不是你的问题。
  • 使用工具箱中的空间布局可能更容易。您可以将它放在按钮上方现有布局的顶部并调整大小,然后它将它们推到底部。
  • 你可以在这篇帖子stackoverflow.com/questions/3989883/…找到很多不同的解决方案

标签: android xml android-layout button android-linearlayout


【解决方案1】:

你需要确保四件事:

  • 你的外部LinearLayoutlayout_height="match_parent"
  • 你的内部LinearLayoutlayout_weight="1"layout_height="0dp"
  • 你的TextViewlayout_weight="0"
  • 你已经在你的内心LinearLayout: android:gravity="center|bottom"上正确设置了重力

请注意,fill_parent 并不意味着“占用所有可用空间”。但是,如果您将layout_height="0dp"layout_weight="1" 一起使用,则视图将占用所有可用空间(Can't get proper layout with "fill_parent")。

这是我快速编写的一些代码,它以与您的代码类似的方式使用两个线性布局。

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/cow"
        android:layout_weight="0"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:gravity="center|bottom"
        android:orientation="vertical" >

        <Button
            android:id="@+id/button1"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="145dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal|center"
            android:text="1" />

        <Button
            android:id="@+id/button2"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="145dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal|center"
            android:text="2" />

        <Button
            android:id="@+id/button3"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="145dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal|center"
            android:text="3" />
    </LinearLayout>

</LinearLayout>

结果看起来像这样:

【讨论】:

  • 投票只是为了说明如何获得视图以填满所有可用空间!谢谢你,很好的答案!
  • 那些 Lint 警告...!
  • 这不是最好的解决方案,因为你不能在剩余屏幕的中心设置视图............ .so 最佳解决方案奖颁给了这个答案 -stackoverflow.com/a/26140793/4741746
  • 您能否描述layout_weight="0" 的意义或它的实际作用?
【解决方案2】:

您可以使用RelativeLayout 并将其底部与android:layout_alignParentBottom="true" 对齐

【讨论】:

  • 不能使用线性布局吗?
  • 是的。就像@AND_DEV 已经说过的:使用layout_weight="1"。这样,您的 LinearLayout 将占据屏幕上剩余的高度;现在您可以将按钮的重力设置为底部。
  • 相对布局很难阅读,几乎从不做你想做的事,而且通常很难维护。在一周中的任何一天,我都会在 1 个 relativelayout 上获得 30 个线性布局的性能损失,仅仅是因为它可以节省数小时令人沮丧的摆弄。
  • @Ahmed 我认为提出的问题对于 LinearLayout 来说非常清楚,但是对于 RelativityLayout 和 Brian Attwell 的回答是 android:gravity="center|bottom" !
  • @Gopi.cs 我真的不确定你为什么要评论我在 6 多年前给出的答案。现在是 2019 年,只需使用 ConstraintLayout。
【解决方案3】:

创建相对布局并在该布局内使用此行创建您的按钮

android:layout_alignParentBottom="true"

【讨论】:

  • 另外添加android:layout_centerHorizontal="true" 使其水平居中,这是一个很好的解决方案。
  • 绝招。谢谢
  • 但为什么我的大多数元素和布局中都没有该属性?
  • 这么简单的解决方案,解决了这么多问题。干杯。
【解决方案4】:

您可以通过将 Frame 布局作为父布局,然后将线性布局放入其中来做到这一点。这是一个例子:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
 >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="5dp"
    android:textSize="16sp"/>


<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="5dp"      
    android:textSize="16sp"
    />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="5dp"
    android:textSize="16sp"/>

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="5dp"
     android:textSize="16sp"/>




</LinearLayout>

<Button
    android:id="@+id/button2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:layout_gravity="bottom"
    />
 </FrameLayout>

【讨论】:

    【解决方案5】:

    首先创建文件名footer.xml 把这段代码放在里面。

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="78dp"
        android:layout_gravity="bottom"
        android:gravity="bottom"
     android:layout_weight=".15"
        android:orientation="horizontal"
        android:background="@drawable/actionbar_dark_background_tile" >
        <ImageView
            android:id="@+id/lborder"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".14"
            android:background="@drawable/action_bar_left_button"
            android:src="@drawable/overlay" />
        <ImageView
            android:id="@+id/unknown"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".14"
            android:background="@drawable/action_bar_left_button"
            android:src="@drawable/notcolor" />
        <ImageView
            android:id="@+id/open"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".14"
            android:background="@drawable/action_bar_left_button"
            android:src="@drawable/openit"
            />
        <ImageView
            android:id="@+id/color"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".14"
            android:background="@drawable/action_bar_left_button"
            android:src="@drawable/colored" />
            <ImageView
            android:id="@+id/rborder"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="@drawable/action_bar_left_button"
            android:src="@drawable/frames"
            android:layout_weight=".14" />
    
    
    </LinearLayout>  
    

    然后创建 header.xml 并将这段代码放入其中。:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="@dimen/action_bar_height"
        android:layout_gravity="top"
        android:baselineAligned="true"
        android:orientation="horizontal"
        android:background="@drawable/actionbar_dark_background_tile" >
        <ImageView
            android:id="@+id/contact"
            android:layout_width="37dp"
            android:layout_height="wrap_content"
            android:layout_gravity="start"
            android:layout_weight=".18"
            android:scaleType="fitCenter"
            android:background="@drawable/action_bar_left_button"
            android:src="@drawable/logo"/>
    
        <ImageView
            android:id="@+id/share"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="start"
            android:layout_weight=".14"
            android:background="@drawable/action_bar_left_button"
            android:src="@drawable/share" />
    
        <ImageView
            android:id="@+id/save"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".14"
            android:background="@drawable/action_bar_left_button"
            android:src="@drawable/save" />
    
        <ImageView
            android:id="@+id/set"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".14"
            android:background="@drawable/action_bar_left_button"
            android:src="@drawable/set" />
    
        <ImageView
            android:id="@+id/fix"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".14"
            android:background="@drawable/action_bar_left_button"
            android:src="@drawable/light" />
    
        <ImageView
            android:id="@+id/rotate"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".14"
            android:background="@drawable/action_bar_left_button"
            android:src="@drawable/ic_menu_rotate" />
    
        <ImageView
            android:id="@+id/stock"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".14"
            android:background="@drawable/action_bar_left_button"
            android:src="@drawable/stock" />
    
    </LinearLayout>
    

    然后在您的main_activity.xml 中并将此代码放入其中:-

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    tools:context=".MainActivity"
    android:id="@+id/relt"
    android:background="@drawable/background" >
    
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="78dp"
        android:id="@+id/down"
        android:layout_alignParentBottom="true" >
    
        <include
            android:layout_width="fill_parent"
            android:layout_height="78dp"
            layout="@layout/footer" >
        </include>
    </LinearLayout>
    <ImageView
        android:id="@+id/view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/down"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/inc"
       >  
        </ImageView> 
        <include layout="@layout/header"
            android:id="@+id/inc"
            android:layout_width="fill_parent"
            android:layout_height="50dp"></include> 
    

    编码愉快 :)

    【讨论】:

    • 顺序重要吗?我注意到您将页脚作为第一个元素,将页眉作为最后一个元素。
    • @MartinKonecny 不,没关系,因为RelativeLayout 有自己的属性,因此您可以控制其他布局适合其中的位置。比如:layout_below等
    【解决方案6】:
    <LinearLayout
     android:id="@+id/LinearLayouts02"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="vertical"
     android:gravity="bottom|end">
    
    <TextView
    android:id="@+id/texts1"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:layout_weight="2"
    android:text="@string/forgotpass"
    android:padding="7dp"
    android:gravity="bottom|center_horizontal"
    android:paddingLeft="10dp"
    android:layout_marginBottom="30dp"
    android:bottomLeftRadius="10dp"
    android:bottomRightRadius="50dp"
    android:fontFamily="sans-serif-condensed"
    android:textColor="@color/colorAccent"
    android:textStyle="bold"
    android:textSize="16sp"
    android:topLeftRadius="10dp"
    android:topRightRadius="10dp"/>
    
    </LinearLayout>
    

    【讨论】:

      【解决方案7】:

      只需将 layout_weight="1" 添加到具有按钮的线性布局中。

      编辑:-让我把它简单化

      如下所示,标签名称可能不正确,这只是一个想法

      <LL>// Top Parrent LinearLayout
         <LL1 height="fill_parent" weight="1" "other tags as requirement"> <TV /><Butons /></LL1> // this layout will fill your screen.
         <LL2 height="wrap_content" weight="1"  orientation="Horizontal" "other tags as requirement"> <BT1 /><BT2/ ></LL2> // this layout gonna take lower part of button height of your screen
      
      <LL/> TOP PARENT CLOSED
      

      【讨论】:

      • 添加 anroid:layout_weight="1" 不会将线性布局(和按钮)向下移动到底部...
      • @AND_DEV,你忘了设置 android:gravity。现在,按钮仍将位于顶部,即使包含它们的 LinearLayout 填充了整个底部区域。
      • 不,按钮将在 LL2 中,并且将在底部区域。如果 OP 想要 Button 在确切的底线,可以设置 Gravity。我只是给出一个粗略的idea,他可以按照他的要求去做。
      • 我没看错,您使用LL1 元素作为分隔符,所以接下来的所有内容都将在底部?相当巧妙的把戏。
      【解决方案8】:

      即使您的父布局是线性的,您也可以将 Button(s) 捆绑在 RelativeLayout 中。确保最外面的父级将 android:layout_height 属性设置为 ma​​tch_parent。 在那个 Button 标签中添加 'android:alignParentBottom="True" '

      【讨论】:

        【解决方案9】:

        android:windowSoftInputMode="adjustPan" 添加到清单 - 到相应的活动中:

          <activity android:name="MyActivity"
            ...
            android:windowSoftInputMode="adjustPan"
            ...
          </activity>
        

        【讨论】:

          【解决方案10】:

          在我的例子中,我试图在线性布局的底部放置一个完整的线性布局。当我尝试使用“layout_gravity”到底部时,它不起作用。所以我把根布局改成了框架布局,效果很好。

          因此,如果您想在底部放置布局,请尝试使用框架布局,并将其他所有内容放在嵌套的线性或相对布局中。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-06-05
            • 1970-01-01
            • 2015-02-11
            • 2016-03-27
            • 2013-10-13
            相关资源
            最近更新 更多