2017-07-27 84 views
2

我想在键盘显示在我的应用程序上时移动我的布局。我使用adjustResize,adjustPan和他们两个人的活动,但他们不工作,它仍然在窗体上显示键盘,并不移动上面的布局。android - 尽管使用adjustResize尽管使用adjustResize键盘显示

这是我的布局:

<?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> 

我怎样才能使它工作?

+0

尝试添加机器人:isScrollContainer = “假” 与Android :windowSoftInputMode =“adjustNothing”或android:windowSoftInputMode =“adjustPan” –

回答

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); 
    } 

andjust添加这个在你的XML
机器人: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> 
+0

感谢您的回复,我应该怎么做onGlobalLayout方法? –

+0

没有什么,这本身就够了。它为我工作。 –

相关问题