DialogFragment 未针对 >= API 24 上的软键盘调整大小
DialogFragment not resizing for soft keyboard on >= API 24
我已经谷歌搜索 Stack Overflow 2 天了,但没有找到可以帮助我的东西。
我正在尝试实现在智能手机上全屏显示并在平板电脑上显示模态对话框的 DialogFragment。
它应该是这样的(在 API 23 级设备上拍摄):
这是它在 API 24 级设备上的样子:
这适用于 API 级别 <= 23 的设备上的以下代码
我在多个真实和模拟设备上对其进行了测试。
TestDialogFragment.java:
public class TestDialogFragment extends DialogFragment
{
private boolean isTablet;
public TestDialogFragment()
{
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
isTablet = getResources().getBoolean(R.bool.isTablet); // defaults to false, true >= sw600dp
if (isTablet)
{
setStyle(DialogFragment.STYLE_NO_TITLE, android.R.style.Theme_Holo_Light_Dialog_NoActionBar_MinWidth);
} else
{
setStyle(STYLE_NO_FRAME, android.R.style.Theme_Holo_Light);
}
setCancelable(false);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle
savedInstanceState)
{
if (!isTablet)
{
Window window = getDialog().getWindow();
if (window != null)
{
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
}
}
return inflater.inflate(R.layout.dialog_fragment, container, false);
}
@Override
public void onStart()
{
super.onStart();
if (isTablet)
{
Dialog d = getDialog();
if (d != null)
{
int width = ViewGroup.LayoutParams.WRAP_CONTENT;
int height = ViewGroup.LayoutParams.MATCH_PARENT;
d.getWindow().setLayout(width, height);
}
}
}
}
dialog_fragment.xml:
<android.support.constraint.ConstraintLayout 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">
<android.support.v7.widget.Toolbar
android:id="@+id/dialog_toolbar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:ellipsize="end"
android:fontFamily="sans-serif-medium"
android:maxLines="2"
android:paddingBottom="14dp"
android:paddingTop="14dp"
android:text="@string/dialog_title"
android:textSize="20sp" />
</android.support.v7.widget.Toolbar>
<EditText
android:id="@+id/edit_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:ems="10"
android:hint="@string/edit_text_1"
android:inputType="text"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/progress_bar"
app:layout_constraintTop_toBottomOf="@+id/dialog_toolbar"
app:theme="@style/AppTheme" />
<ProgressBar
android:id="@+id/progress_bar"
style="?android:progressBarStyle"
android:layout_width="24dp"
android:layout_height="0dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="@id/edit_text"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintLeft_toRightOf="@id/edit_text"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="@id/edit_text"
app:layout_constraintWidth_default="wrap"
tools:visibility="visible" />
<android.support.v7.widget.RecyclerView
android:id="@+id/results_recycler_view"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/edit_text"/>
<TextView
android:id="@+id/no_results_text_view"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="32dp"
android:gravity="center"
android:text="@string/no_results"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/edit_text"
app:layout_constraintVertical_bias="0.0"
app:layout_constraintHeight_default="wrap"
tools:visibility="visible" />
</android.support.constraint.ConstraintLayout>
我不明白为什么这不适用于 API 24 级及更高级别。我在 Android 24.
的发行说明中看到此行为没有变化
我的代码有什么问题还是我忽略了什么?
在此先感谢您的帮助!
我至少设法通过扩展 this class from Mike Penz's MaterialDrawer library 让对话框的内容始终可见。这不是最漂亮的解决方案,但至少可以在打开键盘的情况下与对话框中的所有内容进行交互。
代码如下:
public class KeyboardUtil
{
private View activityDecorView;
private View dialogDecorView;
private View contentView;
private int diff;
//a small helper to allow showing the editText focus
ViewTreeObserver.OnGlobalLayoutListener activityListener = 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.
activityDecorView.getWindowVisibleDisplayFrame(r);
//get screen height and calculate the difference with the usable area from r
int height = activityDecorView.getContext().getResources().getDisplayMetrics().heightPixels;
diff = height - r.bottom;
//no difference means no keyboard, so remove padding
if (diff == 0)
{
//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);
}
}
}
};
ViewTreeObserver.OnGlobalLayoutListener dialogListener = new ViewTreeObserver.OnGlobalLayoutListener()
{
@Override
public void onGlobalLayout()
{
//if it could be a keyboard and the view hasn't shrunk on it's own add the padding to the view
if (diff != 0 && activityDecorView.getContext().getResources().getDisplayMetrics().heightPixels - diff >
contentView.getHeight())
{
//the view shrunk on it's own. remove padding
//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);
}
} else
{
// if the usable 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);
}
}
}
};
public KeyboardUtil(Activity act, View contentView)
{
this.activityDecorView = act.getWindow().getDecorView();
this.contentView = contentView;
//only required on newer android versions. it was working on API level 19
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
{
activityDecorView.getViewTreeObserver().addOnGlobalLayoutListener(activityListener);
}
}
public KeyboardUtil(Activity act, Dialog dialog, View contentView)
{
this(act, contentView);
if (dialog.getWindow() != null)
{
this.dialogDecorView = dialog.getWindow().getDecorView();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
{
dialogDecorView.getViewTreeObserver().addOnGlobalLayoutListener(dialogListener);
}
}
}
public void enable()
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
{
activityDecorView.getViewTreeObserver().addOnGlobalLayoutListener(activityListener);
if (dialogDecorView != null)
{
dialogDecorView.getViewTreeObserver().addOnGlobalLayoutListener(dialogListener);
}
}
}
public void disable()
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
{
activityDecorView.getViewTreeObserver().removeOnGlobalLayoutListener(activityListener);
if (dialogDecorView != null)
{
dialogDecorView.getViewTreeObserver().removeOnGlobalLayoutListener(dialogListener);
}
}
}
/**
* 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);
}
}
}
我已经谷歌搜索 Stack Overflow 2 天了,但没有找到可以帮助我的东西。
我正在尝试实现在智能手机上全屏显示并在平板电脑上显示模态对话框的 DialogFragment。
它应该是这样的(在 API 23 级设备上拍摄):
这是它在 API 24 级设备上的样子:
这适用于 API 级别 <= 23 的设备上的以下代码 我在多个真实和模拟设备上对其进行了测试。
TestDialogFragment.java:
public class TestDialogFragment extends DialogFragment
{
private boolean isTablet;
public TestDialogFragment()
{
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
isTablet = getResources().getBoolean(R.bool.isTablet); // defaults to false, true >= sw600dp
if (isTablet)
{
setStyle(DialogFragment.STYLE_NO_TITLE, android.R.style.Theme_Holo_Light_Dialog_NoActionBar_MinWidth);
} else
{
setStyle(STYLE_NO_FRAME, android.R.style.Theme_Holo_Light);
}
setCancelable(false);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle
savedInstanceState)
{
if (!isTablet)
{
Window window = getDialog().getWindow();
if (window != null)
{
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
}
}
return inflater.inflate(R.layout.dialog_fragment, container, false);
}
@Override
public void onStart()
{
super.onStart();
if (isTablet)
{
Dialog d = getDialog();
if (d != null)
{
int width = ViewGroup.LayoutParams.WRAP_CONTENT;
int height = ViewGroup.LayoutParams.MATCH_PARENT;
d.getWindow().setLayout(width, height);
}
}
}
}
dialog_fragment.xml:
<android.support.constraint.ConstraintLayout 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">
<android.support.v7.widget.Toolbar
android:id="@+id/dialog_toolbar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:ellipsize="end"
android:fontFamily="sans-serif-medium"
android:maxLines="2"
android:paddingBottom="14dp"
android:paddingTop="14dp"
android:text="@string/dialog_title"
android:textSize="20sp" />
</android.support.v7.widget.Toolbar>
<EditText
android:id="@+id/edit_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:ems="10"
android:hint="@string/edit_text_1"
android:inputType="text"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/progress_bar"
app:layout_constraintTop_toBottomOf="@+id/dialog_toolbar"
app:theme="@style/AppTheme" />
<ProgressBar
android:id="@+id/progress_bar"
style="?android:progressBarStyle"
android:layout_width="24dp"
android:layout_height="0dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="@id/edit_text"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintLeft_toRightOf="@id/edit_text"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="@id/edit_text"
app:layout_constraintWidth_default="wrap"
tools:visibility="visible" />
<android.support.v7.widget.RecyclerView
android:id="@+id/results_recycler_view"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/edit_text"/>
<TextView
android:id="@+id/no_results_text_view"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="32dp"
android:gravity="center"
android:text="@string/no_results"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/edit_text"
app:layout_constraintVertical_bias="0.0"
app:layout_constraintHeight_default="wrap"
tools:visibility="visible" />
</android.support.constraint.ConstraintLayout>
我不明白为什么这不适用于 API 24 级及更高级别。我在 Android 24.
的发行说明中看到此行为没有变化我的代码有什么问题还是我忽略了什么?
在此先感谢您的帮助!
我至少设法通过扩展 this class from Mike Penz's MaterialDrawer library 让对话框的内容始终可见。这不是最漂亮的解决方案,但至少可以在打开键盘的情况下与对话框中的所有内容进行交互。
代码如下:
public class KeyboardUtil
{
private View activityDecorView;
private View dialogDecorView;
private View contentView;
private int diff;
//a small helper to allow showing the editText focus
ViewTreeObserver.OnGlobalLayoutListener activityListener = 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.
activityDecorView.getWindowVisibleDisplayFrame(r);
//get screen height and calculate the difference with the usable area from r
int height = activityDecorView.getContext().getResources().getDisplayMetrics().heightPixels;
diff = height - r.bottom;
//no difference means no keyboard, so remove padding
if (diff == 0)
{
//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);
}
}
}
};
ViewTreeObserver.OnGlobalLayoutListener dialogListener = new ViewTreeObserver.OnGlobalLayoutListener()
{
@Override
public void onGlobalLayout()
{
//if it could be a keyboard and the view hasn't shrunk on it's own add the padding to the view
if (diff != 0 && activityDecorView.getContext().getResources().getDisplayMetrics().heightPixels - diff >
contentView.getHeight())
{
//the view shrunk on it's own. remove padding
//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);
}
} else
{
// if the usable 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);
}
}
}
};
public KeyboardUtil(Activity act, View contentView)
{
this.activityDecorView = act.getWindow().getDecorView();
this.contentView = contentView;
//only required on newer android versions. it was working on API level 19
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
{
activityDecorView.getViewTreeObserver().addOnGlobalLayoutListener(activityListener);
}
}
public KeyboardUtil(Activity act, Dialog dialog, View contentView)
{
this(act, contentView);
if (dialog.getWindow() != null)
{
this.dialogDecorView = dialog.getWindow().getDecorView();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
{
dialogDecorView.getViewTreeObserver().addOnGlobalLayoutListener(dialogListener);
}
}
}
public void enable()
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
{
activityDecorView.getViewTreeObserver().addOnGlobalLayoutListener(activityListener);
if (dialogDecorView != null)
{
dialogDecorView.getViewTreeObserver().addOnGlobalLayoutListener(dialogListener);
}
}
}
public void disable()
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
{
activityDecorView.getViewTreeObserver().removeOnGlobalLayoutListener(activityListener);
if (dialogDecorView != null)
{
dialogDecorView.getViewTreeObserver().removeOnGlobalLayoutListener(dialogListener);
}
}
}
/**
* 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);
}
}
}