如何有一个不可见的滚动布局

how to have an invisible scrollable layout

我有一个 Layout.xml,它有可见和 Invisible/Gone 组件。我希望当 Gone Visibility 组件可见时,布局应变为可滚动。

在下面 XML 然后我想让第二个相对布局在它消失的可见性组件变得可见时可以滚动。
Layout.xml

<RelativeLayout
    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"
    xmlns:materialdesign="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/root_view"
    xmlns:roid="http://schemas.android.com/apk/res-auto"
    tools:context=".MainActivity"
    android:background="@color/backgroud_user">


    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="120dp"
        android:src="@drawable/comp_icon" />



    <RelativeLayout
        android:id="@+id/LoginBox"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="20dp"
        android:layout_below="@+id/imageView1"
        android:layout_centerInParent="true">

        <EditText
            android:id="@+id/nameText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="text"
            android:hint="Name"
            android:theme="@style/MyEditTextTheme"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="30dp"
            android:padding="20dp"
            android:textColorHint="#C5CAE9"
            />


        <EditText
            android:layout_below="@+id/nameText"
            android:id="@+id/mobileText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="number"
            android:hint="Mobile Number"
            android:maxLength="10"
            android:theme="@style/MyEditTextTheme"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="30dp"
            android:padding="20dp"
            android:textColorHint="#C5CAE9"
            />

        <EditText
            android:layout_below="@+id/mobileText"
            android:id="@+id/employerText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="text"
            android:hint="Select Employer(s)"
            android:focusable="false"
            android:visibility="gone"
            android:textColor="@color/white"
            android:theme="@style/MyEditTextTheme"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="30dp"
            android:padding="20dp"
            android:textColorHint="#C5CAE9" />

        <com.gc.materialdesign.NewMaterials.Spinner
            android:layout_below="@+id/employerText"
            android:id="@+id/employer_spinner_id"
            style="@style/Material.Widget.Spinner.Light"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:rd_style="@style/Material.Drawable.Ripple.Wave.Light"
            app:rd_enable="true"
            app:rd_delayClick="false"
            app:spn_labelEnable="true"
            app:spn_label="Employer"
            android:visibility="gone"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:paddingTop="20dp"
            android:paddingBottom="20dp"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="30dp"
            app:spn_arrowSwitchMode="true"
            app:spn_arrowAnimDuration="@android:integer/config_shortAnimTime"
            app:spn_arrowInterpolator="@android:anim/decelerate_interpolator"/>

        <LinearLayout
            android:id="@+id/linearlayoutCheckbox"
            android:layout_below="@+id/employer_spinner_id"
            android:layout_width="match_parent"
            android:visibility="gone"
            android:layout_height="wrap_content">

        <com.gc.materialdesign.views.CheckBox
            android:id="@+id/noEmployercheckboxid"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:paddingTop="20dp"
            android:paddingBottom="20dp"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="10dp"
            />

            <TextView
                android:layout_gravity="center"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/white"
                android:text="Employer Unknown"/>
        </LinearLayout>

        <com.gc.materialdesign.views.ButtonRectangle
            android:id="@+id/loginButton"
            android:layout_width="fill_parent"
            android:layout_height="50dp"
            android:text="VALIDATE"
            android:gravity="center|center_horizontal"
            android:layout_below="@+id/linearlayoutCheckbox"
            android:layout_marginTop="7sp"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="30dp"
            android:textColor="#1A237E"/>



    </RelativeLayout>

</RelativeLayout>

请帮我实现。

在 RelativeLayout 上添加一个 ScrollView。 将 Relative 布局的位置属性设置为它(如边距)并将其高度更改为 WRAP_CONTENT.

<RelativeLayout
    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"
    xmlns:materialdesign="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/root_view"
    xmlns:roid="http://schemas.android.com/apk/res-auto"
    tools:context=".MainActivity"
    android:background="@color/backgroud_user">


    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="120dp"
        android:src="@drawable/comp_icon" />


   <ScrollView
         android:id="@+id/scrollView"
         android:width="match_parent"
         android:height="match_parent"
         android:layout_marginTop="20dp"
         android:layout_centerInParent="true"
         android:below="@+id/imageView1">

        <RelativeLayout
            android:id="@+id/LoginBox"
            android:layout_width="match_content"
            android:layout_height="wrap_content">

           ...
        </RelativeLayout>
    </ScrollView>
</RelativeLayout>

在可见模式下需要使用ScrollView滚动relativelayout。事实上,您应该在 ScrollView 上使用 VISIBLEGONE 属性,并将第二个 RelativeLayout 作为其子视图

<RelativeLayout
    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"
    xmlns:materialdesign="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/root_view"
    xmlns:roid="http://schemas.android.com/apk/res-auto"
    tools:context=".MainActivity"
    android:background="@color/backgroud_user">


    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="120dp"
        android:src="@drawable/comp_icon" />

  <ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/linearLayout"
    android:layout_centerInParent="true"
    android:visibility="visible">

    <RelativeLayout
        android:id="@+id/LoginBox"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="20dp"
        android:layout_below="@+id/imageView1"
        android:layout_centerInParent="true">

        <EditText
            android:id="@+id/nameText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="text"
            android:hint="Name"
            android:theme="@style/MyEditTextTheme"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="30dp"
            android:padding="20dp"
            android:textColorHint="#C5CAE9"
            />


        <EditText
            android:layout_below="@+id/nameText"
            android:id="@+id/mobileText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="number"
            android:hint="Mobile Number"
            android:maxLength="10"
            android:theme="@style/MyEditTextTheme"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="30dp"
            android:padding="20dp"
            android:textColorHint="#C5CAE9"
            />

        <EditText
            android:layout_below="@+id/mobileText"
            android:id="@+id/employerText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="text"
            android:hint="Select Employer(s)"
            android:focusable="false"
            android:visibility="gone"
            android:textColor="@color/white"
            android:theme="@style/MyEditTextTheme"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="30dp"
            android:padding="20dp"
            android:textColorHint="#C5CAE9" />

        <com.gc.materialdesign.NewMaterials.Spinner
            android:layout_below="@+id/employerText"
            android:id="@+id/employer_spinner_id"
            style="@style/Material.Widget.Spinner.Light"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:rd_style="@style/Material.Drawable.Ripple.Wave.Light"
            app:rd_enable="true"
            app:rd_delayClick="false"
            app:spn_labelEnable="true"
            app:spn_label="Employer"
            android:visibility="gone"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:paddingTop="20dp"
            android:paddingBottom="20dp"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="30dp"
            app:spn_arrowSwitchMode="true"
            app:spn_arrowAnimDuration="@android:integer/config_shortAnimTime"
            app:spn_arrowInterpolator="@android:anim/decelerate_interpolator"/>

        <LinearLayout
            android:id="@+id/linearlayoutCheckbox"
            android:layout_below="@+id/employer_spinner_id"
            android:layout_width="match_parent"
            android:visibility="gone"
            android:layout_height="wrap_content">

        <com.gc.materialdesign.views.CheckBox
            android:id="@+id/noEmployercheckboxid"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:paddingTop="20dp"
            android:paddingBottom="20dp"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="10dp"
            />

            <TextView
                android:layout_gravity="center"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/white"
                android:text="Employer Unknown"/>
        </LinearLayout>

        <com.gc.materialdesign.views.ButtonRectangle
            android:id="@+id/loginButton"
            android:layout_width="fill_parent"
            android:layout_height="50dp"
            android:text="VALIDATE"
            android:gravity="center|center_horizontal"
            android:layout_below="@+id/linearlayoutCheckbox"
            android:layout_marginTop="7sp"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="30dp"
            android:textColor="#1A237E"/>



    </RelativeLayout>
  </ScrollView>
</RelativeLayout>