使用约束布局,ScrollView和线性布局的Android布局

时间:2018-11-26 06:52:49

标签: android android-layout

我想创建一个包含一个位于表单底部的按钮的表单。我只希望表格可滚动并且按钮保持固定。在较小的显示器上似乎可以正常工作,但是在较大的显示器上,我遇到了问题。这是我的xml布局。

<LinearLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="#fff">
<ImageButton
    android:id="@+id/back_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginTop="4dp"
    android:background="#fff"
    android:src="@drawable/ic_keyboard_backspace_black_24dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<ScrollView xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    <TextView
        android:id="@+id/feestructure"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        android:gravity="start"
        android:textStyle="bold"
        android:text="@string/request_fee_structure"
        android:textColor="@color/colorPrimaryDark"
        android:textSize="20sp"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@+id/school_name_fee"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:gravity="start"
        android:text="@string/komal_day_care_service_and_pre_nursery"
        android:textColor="@color/colorPrimaryDark"
        android:textSize="16sp"
        android:textStyle="bold"
        app:layout_constraintStart_toStartOf="@+id/feestructure"
        app:layout_constraintTop_toBottomOf="@+id/feestructure" />

    <EditText
        android:id="@+id/name_fee"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_marginTop="24dp"
        android:layout_marginEnd="24dp"
        android:background="@drawable/ic_form_background"
        android:elevation="40dp"
        android:hint="@string/name"
        android:inputType="text"
        android:padding="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@+id/school_name_fee"
        app:layout_constraintTop_toBottomOf="@+id/school_name_fee" />

    <EditText
        android:id="@+id/email_fee"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_marginTop="24dp"
        android:background="@drawable/ic_form_background"
        android:hint="@string/email"
        android:inputType="textEmailAddress"
        android:padding="8dp"
        app:layout_constraintEnd_toEndOf="@+id/name_fee"
        app:layout_constraintStart_toStartOf="@+id/name_fee"
        app:layout_constraintTop_toBottomOf="@+id/name_fee" />

    <EditText
        android:id="@+id/phonenumber_fee"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_marginTop="24dp"
        android:background="@drawable/ic_form_background"
        android:hint="@string/phone_number"
        android:inputType="phone"
        android:padding="8dp"
        app:layout_constraintEnd_toEndOf="@+id/email_fee"
        app:layout_constraintStart_toStartOf="@+id/email_fee"
        app:layout_constraintTop_toBottomOf="@+id/email_fee" />

    <EditText
        android:id="@+id/address_fee"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_marginTop="24dp"
        android:background="@drawable/ic_form_background"
        android:hint="@string/address"
        android:inputType="text"
        android:padding="8dp"
        app:layout_constraintEnd_toEndOf="@+id/phonenumber_fee"
        app:layout_constraintStart_toStartOf="@+id/phonenumber_fee"
        app:layout_constraintTop_toBottomOf="@+id/phonenumber_fee" />

    <EditText
        android:id="@+id/age_fee"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_marginTop="24dp"
        android:background="@drawable/ic_form_background"
        android:hint="@string/age_of_the_child"
        android:inputType="text"
        android:padding="8dp"
        app:layout_constraintEnd_toEndOf="@+id/address_fee"
        app:layout_constraintStart_toStartOf="@+id/address_fee"
        app:layout_constraintTop_toBottomOf="@+id/address_fee" />

        <CheckBox
            android:id="@+id/trasportation_fee"
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:layout_marginTop="24dp"
            app:layout_constraintStart_toStartOf="@+id/age_fee"
            app:layout_constraintTop_toBottomOf="@+id/age_fee" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:padding="8dp"
            android:text="@string/is_transportation_required"
            android:textColor="@color/grey"
            android:textSize="16sp"
            app:layout_constraintBottom_toBottomOf="@+id/trasportation_fee"
            app:layout_constraintStart_toEndOf="@+id/trasportation_fee"
            app:layout_constraintTop_toTopOf="@+id/trasportation_fee" />

        <EditText
            android:id="@+id/additional_info_fee"
            android:layout_width="0dp"
            android:layout_height="100dp"
            android:layout_marginTop="24dp"
            android:background="@drawable/ic_form_background"
            android:hint="@string/additional_information"
            android:inputType="text"
            android:padding="8dp"
            app:layout_constraintEnd_toEndOf="@+id/age_fee"
            app:layout_constraintStart_toStartOf="@+id/trasportation_fee"
            app:layout_constraintTop_toBottomOf="@+id/textView2" />
    </android.support.constraint.ConstraintLayout>

    <Button
        android:id="@+id/requestfeestructure_btn_fee"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:background="@color/colorAccent"
        android:padding="4dp"
        android:text="@string/request_fee_structure"
        android:textColor="@color/white"
        android:textSize="14sp"
        android:layout_gravity="bottom"/>
</LinearLayout>
</ScrollView>
</LinearLayout>

这是我运行代码时的屏幕截图。 ON larger Display Google Pixel 2XL

ON smaller Display Google Pixel 2

5 个答案:

答案 0 :(得分:0)

在scrollview中将fillViewport = true添加为:

  <ScrollView xmlns:app="http://schemas.android.com/apk/res-auto"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:fillViewport="true"
   xmlns:android="http://schemas.android.com/apk/res/android"></ScrollView>

android:fillViewport如何工作?

设置为true时,此属性会导致滚动视图的子级在需要时扩展到ScrollView的高度。但是,当子对象比ScrollView高时,该属性将无效。

答案 1 :(得分:0)

android:layout_weight="1"

保留以上结构,并在ScrollView中添加update tblCustomerShop set <<whatever you want to update>> where Customer_id in (select Customer_id from tblCustomer where group_id=5);

答案 2 :(得分:0)

按钮需要在右下角吗?因此,您可以使用ConstraintLayout而不是LinearLayout作为paretn布局,然后使用约束在父级底部设置按钮

在我尝试在项目中使用此布局时,我已经更改了背景,字符串和一些可绘制对象以获得快速解决方案。请进行必要的更改,然后尝试使用此布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff">

    <ImageButton
        android:id="@+id/back_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="4dp"
        android:background="#fff"
        android:src="@drawable/ic_athlete_indicator"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="0dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/back_btn"
        android:layout_height="wrap_content">

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <TextView
                    android:id="@+id/feestructure"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="16dp"
                    android:layout_marginTop="8dp"
                    android:gravity="start"
                    android:text="request_fee_structure"
                    android:textColor="@color/colorPrimaryDark"
                    android:textSize="20sp"
                    android:textStyle="bold"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />

                <TextView
                    android:id="@+id/school_name_fee"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="8dp"
                    android:gravity="start"
                    android:text="komal_day_care_service_and_pre_nursery"
                    android:textColor="@color/colorPrimaryDark"
                    android:textSize="16sp"
                    android:textStyle="bold"
                    app:layout_constraintStart_toStartOf="@+id/feestructure"
                    app:layout_constraintTop_toBottomOf="@+id/feestructure" />

                <EditText
                    android:id="@+id/name_fee"
                    android:layout_width="0dp"
                    android:layout_height="40dp"
                    android:layout_marginEnd="24dp"
                    android:layout_marginTop="24dp"
                    android:background="@drawable/text_field"
                    android:elevation="40dp"
                    android:hint="name"
                    android:inputType="text"
                    android:padding="8dp"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="@+id/school_name_fee"
                    app:layout_constraintTop_toBottomOf="@+id/school_name_fee" />

                <EditText
                    android:id="@+id/email_fee"
                    android:layout_width="0dp"
                    android:layout_height="40dp"
                    android:layout_marginTop="24dp"
                    android:background="@drawable/text_field"
                    android:hint="email"
                    android:inputType="textEmailAddress"
                    android:padding="8dp"
                    app:layout_constraintEnd_toEndOf="@+id/name_fee"
                    app:layout_constraintStart_toStartOf="@+id/name_fee"
                    app:layout_constraintTop_toBottomOf="@+id/name_fee" />

                <EditText
                    android:id="@+id/phonenumber_fee"
                    android:layout_width="0dp"
                    android:layout_height="40dp"
                    android:layout_marginTop="24dp"
                    android:background="@drawable/text_field"
                    android:hint="phone_number"
                    android:inputType="phone"
                    android:padding="8dp"
                    app:layout_constraintEnd_toEndOf="@+id/email_fee"
                    app:layout_constraintStart_toStartOf="@+id/email_fee"
                    app:layout_constraintTop_toBottomOf="@+id/email_fee" />

                <EditText
                    android:id="@+id/address_fee"
                    android:layout_width="0dp"
                    android:layout_height="40dp"
                    android:layout_marginTop="24dp"
                    android:background="@drawable/text_field"
                    android:hint="address"
                    android:inputType="text"
                    android:padding="8dp"
                    app:layout_constraintEnd_toEndOf="@+id/phonenumber_fee"
                    app:layout_constraintStart_toStartOf="@+id/phonenumber_fee"
                    app:layout_constraintTop_toBottomOf="@+id/phonenumber_fee" />

                <EditText
                    android:id="@+id/age_fee"
                    android:layout_width="0dp"
                    android:layout_height="40dp"
                    android:layout_marginTop="24dp"
                    android:background="@drawable/text_field"
                    android:hint="age_of_the_child"
                    android:inputType="text"
                    android:padding="8dp"
                    app:layout_constraintEnd_toEndOf="@+id/address_fee"
                    app:layout_constraintStart_toStartOf="@+id/address_fee"
                    app:layout_constraintTop_toBottomOf="@+id/address_fee" />

                <CheckBox
                    android:id="@+id/trasportation_fee"
                    android:layout_width="wrap_content"
                    android:layout_height="40dp"
                    android:layout_marginTop="24dp"
                    app:layout_constraintStart_toStartOf="@+id/age_fee"
                    app:layout_constraintTop_toBottomOf="@+id/age_fee" />

                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="16dp"
                    android:padding="8dp"
                    android:text="is_transportation_required"
                    android:textColor="@color/blue_e72b3990"
                    android:textSize="16sp"
                    app:layout_constraintBottom_toBottomOf="@+id/trasportation_fee"
                    app:layout_constraintStart_toEndOf="@+id/trasportation_fee"
                    app:layout_constraintTop_toTopOf="@+id/trasportation_fee" />

                <EditText
                    android:id="@+id/additional_info_fee"
                    android:layout_width="0dp"
                    android:layout_height="100dp"
                    android:layout_marginTop="24dp"
                    android:background="@drawable/text_field"
                    android:hint="additional_information"
                    android:inputType="text"
                    android:padding="8dp"
                    app:layout_constraintEnd_toEndOf="@+id/age_fee"
                    app:layout_constraintStart_toStartOf="@+id/trasportation_fee"
                    app:layout_constraintTop_toBottomOf="@+id/textView2" />
            </androidx.constraintlayout.widget.ConstraintLayout>
        </LinearLayout>
    </ScrollView>
    <Button
        android:id="@+id/requestfeestructure_btn_fee"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:background="@color/colorAccent"
        android:padding="4dp"
        app:layout_constraintBottom_toBottomOf="parent"
        android:text="Request fee"
        android:textColor="@color/blue_e72b3990"
        android:textSize="14sp" />
</androidx.constraintlayout.widget.ConstraintLayout>

答案 3 :(得分:0)

尝试一下,希望它能起作用。

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff">

<ImageButton
    android:id="@+id/back_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:background="#fff"
    android:src="@drawable/ic_keyboard_backspace_black_24dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="8dp"
    android:fillViewport="true"
    app:layout_constraintBottom_toTopOf="@+id/requestfeestructure_btn_fee"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/back_btn"
    app:layout_constraintVertical_bias="0.0">

    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/feestructure"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginTop="8dp"
            android:gravity="start"
            android:text="@string/request_fee_structure"
            android:textColor="@color/colorPrimaryDark"
            android:textSize="20sp"
            android:textStyle="bold"
            app:layout_constraintStart_toStartOf="parent" />

        <TextView
            android:id="@+id/school_name_fee"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:gravity="start"
            android:text="@string/komal_day_care_service_and_pre_nursery"
            android:textColor="@color/colorPrimaryDark"
            android:textSize="16sp"
            android:textStyle="bold"
            app:layout_constraintStart_toStartOf="@+id/feestructure"
            app:layout_constraintTop_toBottomOf="@+id/feestructure" />

        <EditText
            android:id="@+id/name_fee"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_marginTop="24dp"
            android:layout_marginEnd="24dp"
            android:background="@drawable/ic_form_background"
            android:elevation="40dp"
            android:hint="@string/name"
            android:inputType="text"
            android:padding="8dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="@+id/school_name_fee"
            app:layout_constraintTop_toBottomOf="@+id/school_name_fee" />

        <EditText
            android:id="@+id/email_fee"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_marginTop="24dp"
            android:background="@drawable/ic_form_background"
            android:hint="@string/email"
            android:inputType="textEmailAddress"
            android:padding="8dp"
            app:layout_constraintEnd_toEndOf="@+id/name_fee"
            app:layout_constraintStart_toStartOf="@+id/name_fee"
            app:layout_constraintTop_toBottomOf="@+id/name_fee" />

        <EditText
            android:id="@+id/phonenumber_fee"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_marginTop="24dp"
            android:background="@drawable/ic_form_background"
            android:hint="@string/phone_number"
            android:inputType="phone"
            android:padding="8dp"
            app:layout_constraintEnd_toEndOf="@+id/email_fee"
            app:layout_constraintStart_toStartOf="@+id/email_fee"
            app:layout_constraintTop_toBottomOf="@+id/email_fee" />

        <EditText
            android:id="@+id/address_fee"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_marginTop="24dp"
            android:background="@drawable/ic_form_background"
            android:hint="@string/address"
            android:inputType="text"
            android:padding="8dp"
            app:layout_constraintEnd_toEndOf="@+id/phonenumber_fee"
            app:layout_constraintStart_toStartOf="@+id/phonenumber_fee"
            app:layout_constraintTop_toBottomOf="@+id/phonenumber_fee" />

        <EditText
            android:id="@+id/age_fee"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_marginTop="24dp"
            android:background="@drawable/ic_form_background"
            android:hint="@string/age_of_the_child"
            android:inputType="text"
            android:padding="8dp"
            app:layout_constraintEnd_toEndOf="@+id/address_fee"
            app:layout_constraintStart_toStartOf="@+id/address_fee"
            app:layout_constraintTop_toBottomOf="@+id/address_fee" />

        <CheckBox
            android:id="@+id/trasportation_fee"
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:layout_marginTop="24dp"
            app:layout_constraintStart_toStartOf="@+id/age_fee"
            app:layout_constraintTop_toBottomOf="@+id/age_fee" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:padding="8dp"
            android:text="@string/is_transportation_required"
            android:textColor="@color/grey"
            android:textSize="16sp"
            app:layout_constraintBottom_toBottomOf="@+id/trasportation_fee"
            app:layout_constraintStart_toEndOf="@+id/trasportation_fee"
            app:layout_constraintTop_toTopOf="@+id/trasportation_fee" />

        <EditText
            android:id="@+id/additional_info_fee"
            android:layout_width="0dp"
            android:layout_height="100dp"
            android:layout_marginTop="24dp"
            android:background="@drawable/ic_form_background"
            android:hint="@string/additional_information"
            android:inputType="text"
            android:padding="8dp"
            app:layout_constraintEnd_toEndOf="@+id/age_fee"
            app:layout_constraintStart_toStartOf="@+id/trasportation_fee"
            app:layout_constraintTop_toBottomOf="@+id/textView2" />


    </android.support.constraint.ConstraintLayout>
</android.support.v4.widget.NestedScrollView>

<Button
    android:id="@+id/requestfeestructure_btn_fee"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:layout_marginBottom="8dp"
    android:background="@color/colorAccent"
    android:padding="4dp"
    android:text="@string/request_fee_structure"
    android:textColor="@color/white"
    android:textSize="14sp"
    app:layout_constraintBottom_toBottomOf="parent" />

答案 4 :(得分:0)

只需添加FrameLayout并将按钮移动到滚动视图之外,希望对您有帮助

type Envelope<'a> =
  { Content : 'a
  ; Errors : string list
  }

let (!>) f e =
    let {Content=content:'a; Errors=errors} = e

    match errors with
    | [] -> {Content = Unchecked.defaultof<_>; Errors = e.Errors }  : Envelope<'b>
    | _ -> f content : Envelope<'b>