【问题标题】:android move layout up when soft keyboard is shown with viewpagers当使用viewpagers显示软键盘时,android将布局向上移动
【发布时间】:2017-04-20 12:40:03
【问题描述】:

好的,我已经在做(我相信)我需要做的一切,以使我的应用程序布局在显示软键盘时滚动/向上移动,但它不工作,所以我猜我一定有什么东西正在阻止它,我想知道如果它的固定高度我给了我的浏览器或我不知道的东西,我通读了将相对布局描述为“粉碎子视图”的帖子,当显示键盘时,线性布局不会粉碎子视图,所以请记住这是我的布局

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 个浏览器,每个浏览器都有不同的布局,第一个(最上面)

第一浏览器

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

第二个浏览器

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

第三浏览器

我希望这个布局在键盘弹出时仍然存在

<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 似乎略微向上移动,但通常顶部两个视图保持在原位,底部一个(我想要显示的)被键盘隐藏 任何帮助表示赞赏

更新 仍然没有进一步,我可以制作具有固定高度的布局,并使用显示的软键盘完美滚动,所以没有问题,但我必须在元素上使用布局中心垂直,这不是我想做的我也试过将其全部包装在滚动视图中并使用 isScrollContainer true 但仍然没有任何乐趣有人有任何想法吗?

【问题讨论】:

  • 键盘出现时FAB是否向上移动?
  • @darshan miskin 不,不是,我不希望风扇在布局中相当高,不应该受到影响

标签: android android-layout android-fragments layout keyboard


【解决方案1】:

我可能弄错了,但这个问题的原因可能是已知的 Android 错误。

所以,首先,您需要在&lt;activity&gt; 标签中添加android:windowSoftInputMode="stateHidden|adjustResize"

其次,你需要将这个类添加到你的项目中:

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

}

然后只需在包含 ViewPagers 的 MainActivity 中调用 assistActivity() 方法即可使用它:

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

更多背景调查this线程。

【讨论】:

  • 嗨@rom4ek 首先谢谢你,这个班级的名字真棒,我把它卡在了,当键盘显示时布局发生变化,但不是我想要的,当键盘隐藏时布局恢复正常,这是你创造的吗?如果是这样,我需要尝试和调整什么才能让这个完美的我猜测它在这条线上的东西 frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;现在我只是以一个空白屏幕结束
  • 所以当你的键盘显示时你有一个空白屏幕,对吧?
  • 它不是空白的,不抱歉,它实际上是我的布局的背景,它持有 viewpagers 我将行更改为这个 frameLayoutParams.height = usableHeightSansKeyboard - heightDifference/2;我仍然可以看到布局顶部的一半(显然)
  • 我还没有想出一个合理的解决方案,但已经授予您正确的解决方法,因为这可能是我的解决方法的开始,感谢您的时间和精力
  • 谢谢。除了我的答案之外,还有一种方法 - 尝试使用 android.support.design.internal.ScrimInsetsFrameLayout 而不是 RelativeLayout 进行滚动行为。没有足够的时间深入探讨您的问题,抱歉。
【解决方案2】:

您可以尝试将android:fitsSystemWindows="true" 添加到您的协调器布局中

【讨论】:

  • 一些关于它会做什么以及应该添加到哪里的评论会很有帮助。
猜你喜欢
  • 2010-12-30
  • 2012-02-25
  • 2011-04-16
  • 1970-01-01
  • 1970-01-01
  • 2015-11-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多