【问题标题】:how to have an invisible scrollable layout如何拥有不可见的可滚动布局
【发布时间】:2015-11-03 07:28:41
【问题描述】:

我有一个 Layout.xml,其中包含可见和不可见/已消失的组件。我希望当 Gone Visibility 组件变得可见时,布局应该变成可滚动的。

在下面的 XML 中,我希望第二个相对布局在其消失的可见性组件变得可见时可滚动。
布局.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>

请帮助我实现 .

【问题讨论】:

  • 使用ScrollView 作为第二个相对布局的父视图

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


【解决方案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>

【讨论】:

    【解决方案2】:

    您需要使用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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-16
      • 1970-01-01
      • 2016-02-04
      • 1970-01-01
      • 2014-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多