【问题标题】:How to scroll layout when keyboard pops up in android如何在android中弹出键盘时滚动布局
【发布时间】:2018-08-01 19:59:05
【问题描述】:

我使用协调器布局制作了以下布局。但是当键盘弹出时,一些元素会在键盘下方,从而失去可见性。 所以请帮我解决这个问题。我尝试将adjustPan,adjustResize放入windowSoftInputMode但仍然无法正常工作。即使尝试使用嵌套滚动视图,也没有成功。

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff">


        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbarLayout"
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:background="@drawable/gradient"
            android:gravity="center">

            <ImageView
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_gravity="center|center_vertical"
                android:background="@drawable/app_main_logo" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center|center_vertical"
                android:layout_marginTop="20dp"
                android:text="My App"
                android:textColor="#fff"
                android:textSize="20dp" />
        </android.support.design.widget.AppBarLayout>

        <android.support.v7.widget.CardView
            android:id="@+id/loginCardView"
            android:layout_width="match_parent"
            android:layout_height="250dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginTop="250dp"
            app:cardCornerRadius="10dp"
            app:cardElevation="8dp">


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:orientation="vertical"
                android:paddingLeft="30dp"
                android:paddingRight="30dp">

                <android.support.design.widget.TextInputLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="8dp"
                    android:layout_marginTop="8dp"
                    android:background="#fff">

                    <EditText
                        android:id="@+id/input_email_login"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginBottom="8dp"
                        android:layout_marginTop="40dp"
                        android:hint="Username"
                        android:inputType="textEmailAddress"
                        android:textColor="#fff"
                        android:textColorHint="#fff" />
                </android.support.design.widget.TextInputLayout>

                <!-- Password Label -->
                <android.support.design.widget.TextInputLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="8dp"
                    android:layout_marginTop="8dp"
                    android:background="#fff"
                    app:passwordToggleEnabled="true">

                    <EditText
                        android:id="@+id/input_password_login"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint="Password"
                        android:inputType="textPassword"
                        android:textColor="#fff"
                        android:textColorHint="#fff" />
                </android.support.design.widget.TextInputLayout>

            </LinearLayout>

        </android.support.v7.widget.CardView>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/capsule_button"
            android:elevation="20dp"
            android:gravity="center"
            android:text="Sign In"
            android:textColor="#fff"
            android:textSize="20dp"
            app:layout_anchor="@id/loginCardView"
            app:layout_anchorGravity="bottom|center" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|center_horizontal"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="Don't have an account?" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginLeft="5dp"
                android:text="Create Now"
                android:textStyle="bold" />
        </LinearLayout>

</android.support.design.widget.CoordinatorLayout>

【问题讨论】:

    标签: java android scrollview android-coordinatorlayout


    【解决方案1】:

    你可以使用 Scrollview,它会启用滚动。所以你可以上下滚动所有布局。试试这个,如果你不想要这个,你也可以检查折叠工具栏。从这里 https://material.io/develop/android/components/collapsing-toolbar-layout/

    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff">
    
         //This is Scrollview you can adjust height and width as you want.
    
        <ScrollView android:layout_marginTop="30dp"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:id="@+id/scrollView">
    
        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbarLayout"
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:background="@drawable/gradient"
            android:gravity="center">
    
            <ImageView
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_gravity="center|center_vertical"
                android:background="@drawable/app_main_logo" />
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center|center_vertical"
                android:layout_marginTop="20dp"
                android:text="My App"
                android:textColor="#fff"
                android:textSize="20dp" />
        </android.support.design.widget.AppBarLayout>
    
        <android.support.v7.widget.CardView
            android:id="@+id/loginCardView"
            android:layout_width="match_parent"
            android:layout_height="250dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginTop="250dp"
            app:cardCornerRadius="10dp"
            app:cardElevation="8dp">
    
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:orientation="vertical"
                android:paddingLeft="30dp"
                android:paddingRight="30dp">
    
                <android.support.design.widget.TextInputLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="8dp"
                    android:layout_marginTop="8dp"
                    android:background="#fff">
    
                    <EditText
                        android:id="@+id/input_email_login"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginBottom="8dp"
                        android:layout_marginTop="40dp"
                        android:hint="Username"
                        android:inputType="textEmailAddress"
                        android:textColor="#fff"
                        android:textColorHint="#fff" />
                </android.support.design.widget.TextInputLayout>
    
                <!-- Password Label -->
                <android.support.design.widget.TextInputLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="8dp"
                    android:layout_marginTop="8dp"
                    android:background="#fff"
                    app:passwordToggleEnabled="true">
    
                    <EditText
                        android:id="@+id/input_password_login"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint="Password"
                        android:inputType="textPassword"
                        android:textColor="#fff"
                        android:textColorHint="#fff" />
                </android.support.design.widget.TextInputLayout>
    
            </LinearLayout>
    
        </android.support.v7.widget.CardView>
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/capsule_button"
            android:elevation="20dp"
            android:gravity="center"
            android:text="Sign In"
            android:textColor="#fff"
            android:textSize="20dp"
            app:layout_anchor="@id/loginCardView"
            app:layout_anchorGravity="bottom|center" />
    
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|center_horizontal"
            android:orientation="horizontal">
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="Don't have an account?" />
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginLeft="5dp"
                android:text="Create Now"
                android:textStyle="bold" />
        </LinearLayout>
        </ScrollView>
        </android.support.design.widget.CoordinatorLayout>
    

    【讨论】:

    • 感谢您的回复,但这不起作用,因为滚动视图只能容纳一个孩子。
    【解决方案2】:

    使用卡片视图到顶部根目录,然后使用其中包含的任何其他布局...!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多