如何在Android键盘弹出时滚动布局

时间:2018-08-01 19:59:06

标签: java android scrollview android-coordinatorlayout

我已经使用协调器布局进行了以下布局。但是,当键盘弹出时,某些元素会移到键盘下方,因此会失去可见性。 所以请帮我解决这个问题。我尝试将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>

2 个答案:

答案 0 :(得分:0)

那么您可以使用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>

答案 1 :(得分:0)

使用卡片视图顶根,然后使用其中的任何其他布局...!