【问题标题】:android - keyboard shows on form despite using adjustResizeandroid - 尽管使用了adjustResize,键盘仍显示在表单上
【发布时间】:2017-07-27 14:04:34
【问题描述】:

当键盘显示在我的应用程序上时,我想将布局向上移动。我使用了adjustResize、adjustPan 和他们两者的活动,但它们都不起作用,它仍然在表单上显示键盘并且不移动上面的布局。

这是我的布局:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

          ............... my views 

        </LinearLayout>
    </ScrollView>
</RelativeLayout>

我怎样才能让它工作?

【问题讨论】:

  • 尝试添加 android:isScrollContainer="false" 与 android:windowSoftInputMode="adjustNothing" 或 android:windowSoftInputMode="adjustPan"

标签: android android-layout android-xml


【解决方案1】:

这样试试

final View activityRootView = findViewById(R.id.activityRoot);
        activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();
                if (heightDiff > dpToPx(getApplicationContext(), 200)) { // if more than 200 dp, it's probably a keyboard...
                    // ... do something here
                }
            }
        });


 public static float dpToPx(Context context, float valueInDp) {
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, valueInDp, metrics);
    }

然后将其添加到您的 xml
android:id="@+id/activityRoot"

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    **android:id="@+id/activityRoot"**>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

          ............... my views 

        </LinearLayout>
    </ScrollView>
</RelativeLayout>

【讨论】:

  • 感谢回复,onGlobalLayout方法应该怎么做?
猜你喜欢
  • 2023-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-20
  • 1970-01-01
  • 2020-05-04
  • 1970-01-01
  • 2016-01-13
相关资源
最近更新 更多