软键盘弹出布局上移

来源:互联网 发布:教学质量分析软件 编辑:程序博客网 时间:2024/05/16 07:04

一、摘要

     公司登录功能让实现弹出软键盘,布局滑动效果使得将登录按钮顶上去,网上查了很多资料,大部分都是添加adjustPan | stateVisible 的属性,但是问题是我的界面效果没什么效果,它只是弹出到输入框的底部,离想要的效果还有很大差异。也有的网友说直接用LinearLayout 的权重 + ScrollView 来实现,但是效果也是不太理想。

二、功能实现

     实现登录页面软键盘顶起功能,主要用了:

     1)ScrollView + LinearLayout 权重效果;

     2)设置需要顶起的控件,然后计算向上滑动距离。

三、布局

      主要实现:使用ScrollView 作为最外层的控件,在布局中使用LinearLayout 的权重效果进行界面的实现

<?xml version="1.0" encoding="utf-8"?><ScrollView xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:id="@+id/scrollView"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:fillViewport="true">    <RelativeLayout        android:id="@+id/rl_login_bg"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:background="@color/white_ffffff">        <ImageView            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:background="@drawable/login_bg"            android:fitsSystemWindows="true"            android:scaleType="fitCenter"></ImageView>        <LinearLayout            android:id="@+id/ll_content"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="vertical">            <LinearLayout                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:layout_weight="1"                android:gravity="center"                android:paddingBottom="45dp"                android:paddingTop="80dp">                <ImageView                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:layout_centerHorizontal="true"                    android:scaleType="fitCenter"                    android:src="@drawable/logo_shb" />            </LinearLayout>            <LinearLayout                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:layout_weight="1"                android:orientation="vertical"                android:paddingTop="70dp">                <android.support.design.widget.TabLayout                    android:id="@+id/tl_login_type"                    android:layout_width="match_parent"                    android:layout_height="wrap_content"                    android:layout_marginLeft="27dp"                    android:layout_marginRight="27dp"                    app:tabGravity="fill"                    app:tabIndicatorColor="@color/blue_01e2e6"                    app:tabIndicatorHeight="2dp"                    app:tabMode="fixed"                    app:tabSelectedTextColor="@color/blue_01e2e6"                    app:tabTextAppearance="@style/CustomTabTextAppearanceStyle"                    app:tabTextColor="@color/gray_666666"                    app:theme="@style/Widget.Design.TabLayout">                    <android.support.design.widget.TabItem                        android:id="@+id/tabitem_commen"                        android:layout_width="match_parent"                        android:layout_height="match_parent"                        android:fadingEdge="none"                        android:text="@string/login_convinent" />                    <android.support.design.widget.TabItem                        android:id="@+id/tabitem_convinent"                        android:layout_width="match_parent"                        android:layout_height="match_parent"                        android:fadingEdge="none"                        android:text="@string/login_commen" />                </android.support.design.widget.TabLayout>                              <RelativeLayout                    android:layout_width="match_parent"                    android:layout_height="wrap_content">                    <android.support.v4.view.ViewPager                        android:id="@+id/vp_login"                        android:layout_width="match_parent"                        android:layout_height="270dp"                        android:layout_marginTop="20dp"></android.support.v4.view.ViewPager>                    <LinearLayout                        android:id="@+id/ll_empty"                        android:layout_width="match_parent"                        android:layout_height="0dp"                        android:layout_marginTop="270dp"                        android:orientation="horizontal"></LinearLayout>                </RelativeLayout>            </LinearLayout>        </LinearLayout>        <LinearLayout            android:id="@+id/ll_back"            android:layout_width="30dp"            android:layout_height="62dp">            <ImageView                android:id="@+id/iv_back"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_marginLeft="15dp"                android:layout_marginTop="32dp"                android:src="@drawable/back_white" />        </LinearLayout>    </RelativeLayout></ScrollView>

四、实现软键盘弹出布局的滑动

      测量软键盘弹出的时候布局滑动的距离。

LayoutUtil.controlKeyboardLayout(rlLoginBg, llEmpty);
public class LayoutUtil {    public static void controlKeyboardLayout(final View root, final View scrollToView) {        root.getViewTreeObserver().addOnGlobalLayoutListener(                new ViewTreeObserver.OnGlobalLayoutListener() {                    @Override                    public void onGlobalLayout() {                        Rect rect = new Rect();                        root.getWindowVisibleDisplayFrame(rect);                        int rootInvisibleHeight = root.getRootView()                                .getHeight() - rect.bottom;                        if (rootInvisibleHeight > 100) {                            int[] location = new int[2];                            scrollToView.getLocationInWindow(location);                            int srollHeight = (location[1] + scrollToView                                    .getHeight()) - rect.bottom;                            root.scrollTo(0, srollHeight);                        } else {                            root.scrollTo(0, 0);                        }                    }                });    }}

五、总结

       目前我的界面软件顶起登录按钮用这种方式实现了,缺少一个点都无法实现,布局顶起的效果是实现了,但是目前我的登录有快捷登录效果,验证码倒计时存在界面闪动问题,目前还在解决中,希望哪位有好的解决方法可以分享下。


原创粉丝点击