2016-12-06 70 views
4

確定我已經在做(我相信)我需要做的一切,使我的應用程序佈局滾動/向上移動時,軟鍵盤顯示,但它不工作,所以即時猜測必須有一些我正在停止它,我想知道它的固定高度,即使給我的視圖頁面或im我不知道的東西,通過閱讀通過職位描述相對佈局爲'粉碎兒童意見',當鍵盤顯示和線性佈局不粉碎孩子的意見,所以考慮到這一點這裏有我的佈局安卓移動佈局時,軟鍵盤顯示與查看器

MAIN_ACTIVITY

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout  
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context=".MainActivity"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior" 
     android:id="@+id/viewpagerHolder"> 

     <android.support.v4.view.ViewPager 
      android:id="@+id/viewpager2" 
      android:layout_width="match_parent" 
      android:layout_marginLeft="8dp" 
      android:layout_marginRight="8dp" 
      android:layout_marginTop="8dp" 
      android:layout_height="@dimen/card_pager_height" /> 

     <RelativeLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/predictsHolder" 
      android:layout_below="@id/viewpager2"> 

     <android.support.v4.view.ViewPager 
      android:id="@+id/viewpager_predicts" 
      android:layout_width="wrap_content" 
      android:layout_marginLeft="8dp" 
      android:layout_marginRight="8dp" 
      android:layout_marginTop="4dp" 
      android:layout_height="@dimen/predicts_pager_height" /> 

     </RelativeLayout> 

     <android.support.design.widget.TabLayout 
      android:id="@+id/tabs" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@id/predictsHolder" 
      app:tabGravity="fill" 
      android:theme="@style/CustomTabLayoutStyle" /> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@id/tabs"> 


     <android.support.v4.view.ViewPager 
      android:id="@+id/viewpager" 
      android:background="@color/windowBackground" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      /> 

     </LinearLayout> 

    </RelativeLayout> 

<android.support.design.widget.AppBarLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" /> 

</android.support.design.widget.AppBarLayout> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_marginTop="@dimen/card_pager_height"> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginRight="12dp" 
     android:layout_marginEnd="12dp" 
     app:elevation="4dp" 
     android:src="@drawable/ic_playlist_play_white_24dp" 
     android:layout_gravity="right|top" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true"/> 

</RelativeLayout> 

</android.support.design.widget.CoordinatorLayout> 

共有3個viewpagers各自具有不同的佈局的第一個(最上面的)

FIRST VIEWPAGER

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@drawable/border" 
android:paddingLeft="2dp" 
android:paddingRight="2dp" 
tools:context=".SpeakGridDB"> 

<android.support.v7.widget.RecyclerView 
    android:id="@+id/card_speak_grid" 
    android:layout_width="match_parent" 
    android:layout_gravity="center" 
    android:layout_height="wrap_content"/> 

</RelativeLayout> 

SECOND VIEWPAGER

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:layout_margin="5dp" 
android:paddingLeft="2dp" 
android:paddingRight="2dp" 
android:background="@drawable/border"> 

<android.support.v7.widget.RecyclerView 
    android:id="@+id/predicts_card_speak_grid" 
    android:layout_width="match_parent" 
    android:layout_gravity="center" 
    android:layout_height="wrap_content"/> 

</RelativeLayout> 

THIRD VIEWPAGER

我想這個佈局仍然存在時,鍵盤會彈出其當前佈局香港專業教育學院的底部,把它放在一個線性佈局,並試圖把它viewpager線性佈局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:orientation="vertical" 
android:background="@color/windowBackground" 
tools:context=".OneFragment"> 

<android.support.v4.widget.SwipeRefreshLayout 
    android:id="@+id/activity_main_swipe_refresh_layout" 
    android:layout_width="match_parent" 
    android:background="@color/windowBackground" 
    android:layout_height="wrap_content"> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/card_grid" 
     android:background="@color/windowBackground" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:clipToPadding="false"/> 

</android.support.v4.widget.SwipeRefreshLayout> 

</LinearLayout> 

我還使用

 android:windowSoftInputMode="adjustResize" 

在我的清單,並也嘗試

 android:windowSoftInputMode="adjustPan" 

,但沒有喜悅它好像tabLayout時微微KEYB上移OARD顯示,但一般前兩名意見留在原處和底部有一個(一個我想所示)由任何幫助表示讚賞

UPDATE 仍然沒有進一步有了這個,我可以做一個佈局鍵盤 隱藏隨着固定的高度,並與顯示的軟鍵盤完全滾動,所以沒有問題,但我必須使用佈局中心垂直的元素,這不是我想做的事情也試圖包裝它在滾動視圖和使用isScrollContainer真的,但仍然沒有快樂的人有任何想法?

+0

當鍵盤出現時,FAB是否向上移動? –

+0

@darshan miskin不,不是我不想要它的風扇是在佈局上相當高,不應該受到影響 –

回答

1

我可就錯了,但這個問題的原因可能是已知的Android的bug。

所以,首先,你需要添加android:windowSoftInputMode="stateHidden|adjustResize"<activity>標籤內。

其次,則需要將該類添加到您的項目:通過調用assistActivity()方法在MainActivity持有ViewPagers

public class AndroidBug5497Workaround { 

    // For more information, see https://code.google.com/p/android/issues/detail?id=5497 
    // To use this class, simply invoke assistActivity() on an Activity that already has its content view set. 

    public static void assistActivity (Activity activity) { 
     new AndroidBug5497Workaround(activity); 
    } 
    private View mChildOfContent; 
    private int usableHeightPrevious; 
    private FrameLayout.LayoutParams frameLayoutParams; 

    private AndroidBug5497Workaround(Activity activity) { 
     FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content); 
     mChildOfContent = content.getChildAt(0); 
     mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 
      public void onGlobalLayout() { 
       possiblyResizeChildOfContent(); 
      } 
     }); 
     frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams(); 
    } 

    private void possiblyResizeChildOfContent() { 
     int usableHeightNow = computeUsableHeight(); 
     if (usableHeightNow != usableHeightPrevious) { 
      int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight(); 
      int heightDifference = usableHeightSansKeyboard - usableHeightNow; 
      if (heightDifference > (usableHeightSansKeyboard/4)) { 
       // keyboard probably just became visible 
       frameLayoutParams.height = usableHeightSansKeyboard - heightDifference; 
       } else { 
       // keyboard probably just became hidden 
       frameLayoutParams.height = usableHeightSansKeyboard; 
      } 
      mChildOfContent.requestLayout(); 
      usableHeightPrevious = usableHeightNow; 
     } 
    } 

    private int computeUsableHeight() { 
     Rect r = new Rect(); 
     mChildOfContent.getWindowVisibleDisplayFrame(r); 
     return (r.bottom - r.top); 
    } 

} 

,然後簡單地使用它:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(...); 
    AndroidBug5497Workaround.assistActivity(this); 
    ... 
} 

欲瞭解更多背景檢查this線程。

+0

hi @ rom4ek首先謝謝你,這個類的偉大名字lol ive卡住了它,當鍵盤顯示時,佈局變化,但不是我想要的,當鍵盤被隱藏時佈局恢復正常,您是否創建了這個?如果是這樣我需要嘗試和調整,以得到這個完美的即時通訊猜測它的東西在這一行frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;現在我只是結束了一個空白的屏幕 –

+0

所以當你的鍵盤顯示你有一個空白屏幕的權利? – rom4ek

+0

這不是空白沒有抱歉它實際上是我的佈局的背景持有的視圖頁我改變了這一行frameLayoutParams.height = usableHeightSansKeyboard - heightDifference/2;我仍然可以看到我的佈局的一半(顯然) –

0

你可以嘗試添加android:fitsSystemWindows="true"到您的協調佈局

+0

一些評論什麼會做什麼,應該添加將有所幫助。 –

+0

謝謝我試過這個,但沒有變化 –