机器人:windowSoftInputMode =" adjustResize"没有任何区别?

时间:2011-12-06 10:04:48

标签: android android-layout android-keypad android-input-method

我有一个使用此布局的虚假对话框:

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

    <FrameLayout    android:id="@+id/dialogHolder"
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    android:padding="15dp"
                    android:layout_gravity="center"
                    android:background="@drawable/panel_picture_frame_bg_focus_blue"/>    
</FrameLayout> 

我在<FrameLayout>内放置一个片段,具体取决于打开的对话框 - 控制Dialog的活动如下所示:

<activity
    android:label="@string/app_name"
    android:name=".activity.DialogActivity"
    android:theme="@style/CustomTheme.Screen.Transparent" 
    android:windowSoftInputMode="adjustResize">

不幸的是,当您单击对话框内的编辑文本时,不会进行大小调整。 windowSoftInputMode字面上没有区别,因为功能与平移模式相同。

Documentation说“这当然只适用于具有可调整区域的应用程序,可以缩小以留出足够的空间”,但不会告诉你“可调整区域”意味着什么并让我思考在某种程度上我有一个可调整大小的区域?

如果有人知道什么事可以帮助我吗?

修改

如此围绕对话框不会改变任何内容:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/containerPageConatiner"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <View
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <FrameLayout    
        android:id="@+id/dialogHolder"
        android:layout_height="wrap_content"
    android:layout_width="wrap_content"
        android:padding="15dp"
        android:layout_gravity="center"
        android:background="@drawable/panel_picture_frame_bg_focus_blue"/>

    <View
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="1" />
</LinearLayout>

EDIT2

作为父母的Scrollview也无济于事:

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

    <FrameLayout
            android:id="@+id/dialogHolder"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:padding="15dp" />

</ScrollView>

9 个答案:

答案 0 :(得分:47)

我创建了一个新项目,以便尝试获取用于窗口大小调整的基本功能,并慢慢将其移向我项目的目标。这样做我跟踪了这​​个问题:

在我的主题层次结构中,我有这个属性:

<item name="android:windowFullscreen">true</item> 

被埋葬在Theme.Black.NoTitleBar.FullScreen的水平 - 我自定义主题的祖先。

documentation表示这是一个“指示此窗口是否应填满整个屏幕的标志”。如果你有一个占据整个屏幕的应用程序,这听起来是件好事......除了它仍然占据整个屏幕而没有旗帜。

事实上,一旦你把它拿出来,应用程序根本就没有变化......除了adjustResize现在效果很好。

答案 1 :(得分:45)

前段时间我在我创建的图书馆中也遇到了同样的问题。 (MaterialDrawer

据我所知,所有提供的答案都没有解决主要问题,他们只是指出删除全屏标志(android:windowFullscreen),这对许多人来说都不是解决办法。

上述&#34;问题&#34;仅出现在以API Level 19(KITKAT)开头的Android版本中,因为它们更改了行为。为了正确,它没有问题,它按预期工作。查看Android员工的评论(Android-Issue)。

所以我开始挖掘Android源代码并使用ViewTreeObserver.OnGlobalLayoutListener找到以下解决方案,并在键盘显示/隐藏时作出反应。如果显示键盘,我将填充添加到容器视图的底部,然后将模拟与adjustResize相同的内容。

解决方案

为简化用法,我将整个事情包装在一个简单的KeyboardUtil助手类中。

/**
 * Created by mikepenz on 14.03.15.
 * This class implements a hack to change the layout padding on bottom if the keyboard is shown
 * to allow long lists with editTextViews
 * Basic idea for this solution found here: http://stackoverflow.com/a/9108219/325479
 */
public class KeyboardUtil {
    private View decorView;
    private View contentView;

    public KeyboardUtil(Activity act, View contentView) {
        this.decorView = act.getWindow().getDecorView();
        this.contentView = contentView;

        //only required on newer android versions. it was working on API level 19 (Build.VERSION_CODES.KITKAT)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            decorView.getViewTreeObserver().addOnGlobalLayoutListener(onGlobalLayoutListener);
        }
    }

    public void enable() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            decorView.getViewTreeObserver().addOnGlobalLayoutListener(onGlobalLayoutListener);
        }
    }

    public void disable() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            decorView.getViewTreeObserver().removeOnGlobalLayoutListener(onGlobalLayoutListener);
        }
    }


    //a small helper to allow showing the editText focus
    ViewTreeObserver.OnGlobalLayoutListener onGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            Rect r = new Rect();
            //r will be populated with the coordinates of your view that area still visible.
            decorView.getWindowVisibleDisplayFrame(r);

            //get screen height and calculate the difference with the useable area from the r
            int height = decorView.getContext().getResources().getDisplayMetrics().heightPixels;
            int diff = height - r.bottom;

            //if it could be a keyboard add the padding to the view
            if (diff != 0) {
                // if the use-able screen height differs from the total screen height we assume that it shows a keyboard now
                //check if the padding is 0 (if yes set the padding for the keyboard)
                if (contentView.getPaddingBottom() != diff) {
                    //set the padding of the contentView for the keyboard
                    contentView.setPadding(0, 0, 0, diff);
                }
            } else {
                //check if the padding is != 0 (if yes reset the padding)
                if (contentView.getPaddingBottom() != 0) {
                    //reset the padding of the contentView
                    contentView.setPadding(0, 0, 0, 0);
                }
            }
        }
    };


    /**
     * Helper to hide the keyboard
     *
     * @param act
     */
    public static void hideKeyboard(Activity act) {
        if (act != null && act.getCurrentFocus() != null) {
            InputMethodManager inputMethodManager = (InputMethodManager) act.getSystemService(Activity.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(act.getCurrentFocus().getWindowToken(), 0);
        }
    }
}

然后,您可以通过执行以下操作在活动或片段中使用它:

//initialize the KeyboardUtil (you can do this global)
KeyboardUtil keyboardUtil = new KeyboardUtil(activity, getContent().getChildAt(0));

//enable it
keyboardUtil.enable();

//disable it
keyboardUtil.disable();

整个util类在上面提到的库MaterialDrawer中使用,可以在KeyboardUtil找到。这将始终包含最新版本。 (如果有改进)

答案 2 :(得分:2)

似乎问题在于FrameLayout,因为它的行为方式是每个孩子占据该帧的可见空间,因此无需调整大小以适应孩子。 尝试使用RelativeLayout。它应该工作。

答案 3 :(得分:0)

尝试将LinearLayout放在ScrollView上,这对我有用..

答案 4 :(得分:0)

我没有使用ScrollView作为我的父母,我刚刚将android:fitsSystemWindows="true"添加到我的父视图(对于活动的清单RelativeLayoutadjustResize,它工作正常

答案 5 :(得分:0)

我必须设置

<item name="android:windowFullscreen">false</item> 

尽管如此,我从未将其设置为真,而且应用实际上并非全屏。

答案 6 :(得分:0)

我不知道原因,但如果您的主题中有<item name="android:windowTranslucentNavigation">true</item>,请将其更改为false。它将开始工作。真奇怪。

答案 7 :(得分:-1)

当原始海报在将Fullscreen Flag分配给某个活动时发现,android:windowFullscreen属性将不起作用,因此当软键盘可见且不可滚动时,您的窗口将不会调整大小。

只需删除全屏标记而不使用全屏主题,即可在软键盘可见时滚动。

答案 8 :(得分:-3)

确保在样式中将windowTranslucentStatus设置为false

相关问题