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

时间:2015-11-03 07:28:42

标签: android android-layout android-activity android-xml

我有一个Layout.xml,它有可见和不可见/ 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>

请帮我实现。

2 个答案:

答案 0 :(得分:1)

在RelativeLayout上添加ScrollView。 设置相对布局的位置属性(如边距)并将其高度更改为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>

答案 1 :(得分:1)

当处于可见模式时,您需要使用ScrollView滚动relativelayout。实际上,您应该在VISIBLE上使用GONEScrollView属性,并将其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>