首页 > 解决方案 > 有没有办法在线性布局中从底部堆叠小部件?

问题描述

定义 aLinearlayout时,像Button,这样的小部件Edittext将从顶部开始堆叠。例如,下面的代码将像这样显示

    <LinearLayout

        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:text="button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <EditText
            android:text="edittext"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <Button
            android:text="button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <EditText
            android:text="edittext"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>

是否可以不使用从布局底部开始添加按钮和编辑文本layout_weight

标签: androidlayoutandroid-linearlayout

解决方案


添加android:gravity="bottom"到您的根布局:

<LinearLayout

    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="bottom" \\ add this line
    android:orientation="vertical">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="edittext" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="edittext" />

</LinearLayout>

推荐阅读