【问题标题】:How to put the button really at the bottom如何将按钮真正放在底部
【发布时间】:2011-10-29 08:50:31
【问题描述】:

我正在尝试在线性布局的底部放置一些按钮。 该按钮只出现在文本之后,我希望它出现在文本之后但在底部。 这是代码:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="30dip"
android:background="@color/background">

<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_gravity="center" android:layout_height="match_parent">

<TextView
android:id="@+id/test"
android:text="This is a test"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="25dip"
android:textSize="16.5sp" />


 <Button
 android:layout_height="wrap_content"
 android:layout_width="wrap_content"
 android:layout_gravity="bottom"
 android:text="continue"

 android:id="@+id/continuebutton"
  />

 </LinearLayout>

 </LinearLayout>

【问题讨论】:

    标签: android xml layout button


    【解决方案1】:

    使用LinearLayout,你唯一能做的就是:

    <TextView
        android:id="@+id/test"
        android:layout_width="wrap_content"
        android:layout_height="0dp" <---
        android:layout_gravity="center"
        android:layout_marginBottom="25dip"
        android:layout_weight="1" <---
        android:text="This is a test"
        android:textSize="16.5sp" />
    
    <Button
        android:id="@+id/continuebutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" <---
        android:layout_gravity="bottom"
        android:layout_weight="0" <---
        android:text="continue" />
    

    请检查以下行:

    当然,最好的方法可能是使用 RelativeLayout 并让按钮 alignParentBottom 像这样:

    <RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:orientation="vertical" >
    
    <TextView
        android:id="@+id/test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginBottom="25dip"
        android:text="This is a test"
        android:textSize="16.5sp" />
    
    <Button
        android:id="@+id/continuebutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="continue" android:layout_alignParentBottom="true"/>
    </RelativeLayout>
    

    希望这会有所帮助!

    【讨论】:

    • 它有帮助,但是否可以将按钮向上移动一点,向右移动?
    • 取决于高度:您应该添加 margin_bottom - 即 20dp。取决于水平定位:如果需要居中,则应设置 centerInHorizo​​ntal true,否则应设置 margin_left 或 margin_right。
    猜你喜欢
    • 2012-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-16
    • 1970-01-01
    • 2015-04-02
    • 1970-01-01
    • 2021-12-10
    相关资源
    最近更新 更多