簡體   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