繁体   English   中英

如何在 ScrollView 内的 LinearLayout 底部放置按钮

[英]How to place button at the bottom of LinearLayout within a ScrollView

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM