android:windowSoftInputMode ="adjustResize"没有任何区别?

Gra*_*eme 28 android android-keypad android-layout 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> 
Run Code Online (Sandbox Code Playgroud)

<FrameLayout>根据打开的对话框放置一个片段- 控制Dialog的活动如下所示:

<activity
    android:label="@string/app_name"
    android:name=".activity.DialogActivity"
    android:theme="@style/CustomTheme.Screen.Transparent" 
    android:windowSoftInputMode="adjustResize">
Run Code Online (Sandbox Code Playgroud)

不幸的是,当您单击对话框内的编辑文本时,不会进行大小调整.在windowSoftInputMode字面上没有什么区别的功能是一样的全景模式.

文档说"这当然只适用于具有可调整区域的应用程序,可以缩小以留出足够的空间",但不会告诉您"可调整区域"的含义,并让我认为在某种程度上我喜欢Ť有一个可调整大小的面积?

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

编辑

如此围绕对话框不会改变任何东西:

<?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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

Gra*_*eme 50

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

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

<item name="android:windowFullscreen">true</item> 
Run Code Online (Sandbox Code Playgroud)

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

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

事实上,一旦你把它拿出来,应用程序根本就没有变化......除了adjustResize现在完美无缺.

  • 谢谢你的帖子!但我想要全屏和调整大小.:( (4认同)
  • 重新阅读答案.无论`windowFullscreen`设置为true,false还是不存在,您的Activity仍将是全屏. (3认同)
  • 当我从我的自定义主题中删除android:windowFullscreen时,它会不幸地停止全屏(os 4.1).你能发布你主题的完整代码吗?也许我的实现中缺少一些东西. (3认同)

mik*_*enz 46

前段时间我在我创建的库中也有同样的问题.(MaterialDrawer)

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

上述"问题"仅出现在以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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

//enable it
keyboardUtil.enable();

//disable it
keyboardUtil.disable();
Run Code Online (Sandbox Code Playgroud)

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