2017-07-27 85 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

沒有什麼,這本身就夠了。它爲我工作。 –

相關問題