键盘显示时禁用对话框片段
Disable dialog fragment up when keyboard show
粉红色的是DialogFragment。当键盘显示时,它会上升并与工具栏重叠。我怎样才能让对话框片段保持不变,只有编辑文本上升。
我使用了 SOFT_INPUT_ADJUST_PAN 或 SOFT_INPUT_ADJUST_RESIZE 但片段仍然上升
SOFT_INPUT_ADJUST_NOTHING:对话框和编辑文本都保持不变,但我希望编辑文本上升
任何帮助将不胜感激?
MainActivity.java:
public class HelloDialogFragment extends DialogFragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.dialog_fragment, container, false);
return v;
}
@Override
public void onStart() {
super.onStart();
Dialog dialog = getDialog();
if (dialog != null && dialog.getWindow() != null) {
Window window = dialog.getWindow();
TypedValue tv = new TypedValue();
getContext().getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true);
int actionBarHeight = getResources().getDimensionPixelSize(tv.resourceId);
int statusBarHeight = (int) getResources().getDimension(R.dimen.status_bar_height);
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
WindowManager.LayoutParams params = window.getAttributes();
params.gravity = Gravity.LEFT | Gravity.TOP;
params.x = 200 + 2 * Utils.dpToPx(8);
params.y = statusBarHeight + actionBarHeight + Utils.dpToPx(8);
params.width = Utils.screenWidth() / 3;
params.height = Utils.screenHeight() - statusBarHeight - actionBarHeight - 2 * Utils.dpToPx(8);
window.setAttributes(params);
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
}
}
static class Utils {
public static int screenWidth() {
return Resources.getSystem().getDisplayMetrics().widthPixels;
}
public static int screenHeight() {
return Resources.getSystem().getDisplayMetrics().heightPixels;
}
public static int dpToPx(int dp) {
return (int) (dp * Resources.getSystem().getDisplayMetrics().density);
}
}
}
dialog_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f2f"
android:orientation="vertical">
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#E8E8E8"
android:gravity="center_vertical"
android:orientation="horizontal">
<EditText
android:id="@+id/chatEditText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_weight="1"
android:background="#fff"
android:hint="Enter"
android:padding="8dp"
android:textColor="#B0B0B0"
android:textDirection="locale"/>
<Button
android:id="@+id/sendButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:background="@color/colorPrimary"
android:padding="8dp"
android:text="SEND"
android:textColor="#fff"/>
</LinearLayout>
</LinearLayout>
只是说说我的想法。
您是否尝试过用 FrameLayout
等另一种布局将 dialog_fragment.xml
中的所有元素包装起来,然后将 paddingTop
设置为等于或大于 ToolBar
[= 的大小18=]
或
将根元素的 marginTop
设置为等于或大于 ToolBar
的大小
我不知道,如果这会有所帮助。
这里是FrameLayout
包裹元素的代码
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:android:paddingTop=toolBarSize/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f2f"
android:orientation="vertical">
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#E8E8E8"
android:gravity="center_vertical"
android:orientation="horizontal">
<EditText
android:id="@+id/chatEditText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_weight="1"
android:background="#fff"
android:hint="Enter"
android:padding="8dp"
android:textColor="#B0B0B0"
android:textDirection="locale"/>
<Button
android:id="@+id/sendButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:background="@color/colorPrimary"
android:padding="8dp"
android:text="SEND"
android:textColor="#fff"/>
</LinearLayout>
</LinearLayout>
</FrameLayout>
最后我发现了一个实现它的技巧
首先,使 Dialog 片段高度与父级匹配
其次,在视图顶部添加一个透明视图(高度等于工具栏)。因此,当键盘显示时,对话框将调整大小但不会向上移动并与工具栏重叠
粉红色的是DialogFragment。当键盘显示时,它会上升并与工具栏重叠。我怎样才能让对话框片段保持不变,只有编辑文本上升。
我使用了 SOFT_INPUT_ADJUST_PAN 或 SOFT_INPUT_ADJUST_RESIZE 但片段仍然上升
SOFT_INPUT_ADJUST_NOTHING:对话框和编辑文本都保持不变,但我希望编辑文本上升
任何帮助将不胜感激?
MainActivity.java:
public class HelloDialogFragment extends DialogFragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.dialog_fragment, container, false);
return v;
}
@Override
public void onStart() {
super.onStart();
Dialog dialog = getDialog();
if (dialog != null && dialog.getWindow() != null) {
Window window = dialog.getWindow();
TypedValue tv = new TypedValue();
getContext().getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true);
int actionBarHeight = getResources().getDimensionPixelSize(tv.resourceId);
int statusBarHeight = (int) getResources().getDimension(R.dimen.status_bar_height);
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
WindowManager.LayoutParams params = window.getAttributes();
params.gravity = Gravity.LEFT | Gravity.TOP;
params.x = 200 + 2 * Utils.dpToPx(8);
params.y = statusBarHeight + actionBarHeight + Utils.dpToPx(8);
params.width = Utils.screenWidth() / 3;
params.height = Utils.screenHeight() - statusBarHeight - actionBarHeight - 2 * Utils.dpToPx(8);
window.setAttributes(params);
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
}
}
static class Utils {
public static int screenWidth() {
return Resources.getSystem().getDisplayMetrics().widthPixels;
}
public static int screenHeight() {
return Resources.getSystem().getDisplayMetrics().heightPixels;
}
public static int dpToPx(int dp) {
return (int) (dp * Resources.getSystem().getDisplayMetrics().density);
}
}
}
dialog_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f2f"
android:orientation="vertical">
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#E8E8E8"
android:gravity="center_vertical"
android:orientation="horizontal">
<EditText
android:id="@+id/chatEditText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_weight="1"
android:background="#fff"
android:hint="Enter"
android:padding="8dp"
android:textColor="#B0B0B0"
android:textDirection="locale"/>
<Button
android:id="@+id/sendButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:background="@color/colorPrimary"
android:padding="8dp"
android:text="SEND"
android:textColor="#fff"/>
</LinearLayout>
</LinearLayout>
只是说说我的想法。
您是否尝试过用 FrameLayout
等另一种布局将 dialog_fragment.xml
中的所有元素包装起来,然后将 paddingTop
设置为等于或大于 ToolBar
[= 的大小18=]
或
将根元素的 marginTop
设置为等于或大于 ToolBar
我不知道,如果这会有所帮助。
这里是FrameLayout
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:android:paddingTop=toolBarSize/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f2f"
android:orientation="vertical">
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#E8E8E8"
android:gravity="center_vertical"
android:orientation="horizontal">
<EditText
android:id="@+id/chatEditText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_weight="1"
android:background="#fff"
android:hint="Enter"
android:padding="8dp"
android:textColor="#B0B0B0"
android:textDirection="locale"/>
<Button
android:id="@+id/sendButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:background="@color/colorPrimary"
android:padding="8dp"
android:text="SEND"
android:textColor="#fff"/>
</LinearLayout>
</LinearLayout>
</FrameLayout>
最后我发现了一个实现它的技巧 首先,使 Dialog 片段高度与父级匹配 其次,在视图顶部添加一个透明视图(高度等于工具栏)。因此,当键盘显示时,对话框将调整大小但不会向上移动并与工具栏重叠