【问题标题】:How to place button at the bottom of LinearLayout within a ScrollView如何在 ScrollView 内的 LinearLayout 底部放置按钮
【发布时间】:2020-11-09 08:40:51
【问题描述】:

我创建了一个 addview (xml)。当我按下添加按钮时,它会在 LinearLayout 中创建一个新按钮。添加按钮放置在同一个线性布局中。但是当我放置添加按钮时,它保持在相同的位置。但我希望按钮位于添加视图的底部。这是我的 xml。

    <ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/banner"
    android:layout_above="@+id/linearmenu"
    android:layout_centerHorizontal="true">

    <LinearLayout
        android:id="@+id/linearLayout_addhere"
        android:layout_width="match_parent"
        android:layout_gravity="top"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <Button
            android:id="@+id/buttonAdd"
            android:layout_width="wrap_content"
            android:layout_height="35dp"
            android:layout_gravity="center"
            android:layout_marginTop="10dp"
            android:background="@drawable/add_button_design"
            android:drawableRight="@drawable/ic_add_white"
            android:paddingHorizontal="7dp"
            android:text="추가"
            android:textColor="#FFFFFF"
            android:textSize="18sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent" />


    </LinearLayout>

</ScrollView>

【问题讨论】:

    标签: android android-layout scrollview android-linearlayout


    【解决方案1】:

    使用 LinearLayout

    试试这种方式
    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/linearmenu"
        android:layout_below="@+id/banner"
        android:layout_centerHorizontal="true"
        android:fillViewport="true">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
    
            <LinearLayout
                android:id="@+id/linearLayout_addhere"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:orientation="vertical"
                android:layout_weight="1">
    
            </LinearLayout>
    
            <Button
                android:id="@+id/buttonAdd"
                android:layout_width="match_parent"
                android:layout_height="35dp"
                android:layout_gravity="bottom"
                android:layout_marginTop="10dp"
                android:paddingHorizontal="7dp"
                android:text="추가"
                android:textColor="#FFFFFF"
                android:textSize="18sp" />
    
    
        </LinearLayout>
    
    </ScrollView>
    

    使用 RelativeLayout

    试试这种方式

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        
    
        <Button
            android:id="@+id/buttonAdd"
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_gravity="bottom"
            android:layout_marginTop="10dp"
            android:layout_alignParentBottom="true"
            android:paddingHorizontal="7dp"
            android:text="추가"
            android:textColor="#FFFFFF"
            android:textSize="18sp" />
    
    
    </RelativeLayout>
    

    使用 ConstraintLayout

    试试这种方式
    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_above="@+id/linearmenu"
        android:layout_below="@+id/banner"
        android:layout_centerHorizontal="true"
        android:fillViewport="true">
    
        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
    
    
            <Button
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                android:id="@+id/buttonAdd"
                android:layout_width="match_parent"
                android:layout_height="35dp"
                android:layout_gravity="bottom"
                android:layout_marginTop="10dp"
                android:layout_alignParentBottom="true"
                android:paddingHorizontal="7dp"
                android:text="추가"
                android:textColor="#FFFFFF"
                android:textSize="18sp" />
    
    
        </androidx.constraintlayout.widget.ConstraintLayout>
    
    </ScrollView>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-02
      • 2021-04-08
      • 1970-01-01
      相关资源
      最近更新 更多