2015-11-03 125 views
0

我有一個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> 

請幫我實現。

+0

使用'ScrollView'作爲第二相對佈局 – Shvet

回答

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> 
+0

感謝名單了很多父視圖。它爲我工作。 –

1

您需要使用ScrollView來滾動relativelayout,當它處於可見模式時。事實上,你應該ScrollView使用VISIBLEGONE屬性並進行第二RelativeLayout其childview

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