我正在编写我的第一个真正的Android应用程序。我有一个Android DialogFragment
我正在显示,以允许用户添加或编辑所选项目的详细信息。 DialogFragment弹出并以全屏模式显示。但是,在Lollipop模拟器上单击屏幕下方的EditText
窗口小部件时,软键盘会覆盖窗口小部件,而不是向上滚动屏幕以便用户可以看到他们正在键入的内容。我没有在pre-Lollipop模拟器中遇到这种行为。在KitKat模拟器上,屏幕会进行调整,以便EditText
仍然可见。 StackOverflow不允许我发布图片。所以,你必须运用你的想象力。 :-(我已经尝试了不同的方法来克服这个问题。不幸的是,没有一个方法可以用于Lollipop。
更新 - 我在运行Android 5.0.2的Sony Xperia Z3 Compact上进行了测试,DialogFragment
在选择较低的{{1}时不会滚动或调整屏幕小部件。
以下是使用的样式:
EditText
正如您在<!-- Base application theme. -->
<style name="AppTheme" parent="AppTheme.Base">
<!-- Customize your theme here. -->
</style>
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:textColorPrimary">@color/colorPrimaryText</item>
<item name="android:textColorSecondary">@color/colorSecondaryText</item>
<item name="android:dialogTheme">@style/DialogTheme</item>
<item name="android:actionBarStyle">@style/AppActionBarTheme</item>
<item name="actionBarStyle">@style/AppActionBarTheme</item>"
<item name="actionBarTheme">@style/AppActionBarTheme</item>
<item name="actionBarPopupTheme">@style/AppActionBarOverflowMenuTheme</item>
<item name="windowActionModeOverlay">true</item>
</style>
<style name="DialogTheme" parent="Theme.AppCompat.Light.Dialog">
<item name="android:clickable">true</item>
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowSoftInputMode">stateHidden|adjustResize</item>
<item name="windowActionModeOverlay">true</item>
</style>
中看到的,我已添加了DialogTheme
属性。我还尝试在最终包含android:windowSoftInputMode
的Activity下的AndroidManifest.xml文件中使用该属性。以下是DialogFragment
:
DialogFragment
如您所见,我的布局包含<?xml version="1.0" encoding="utf-8"?>
<com.ahappydevelopment.android.fishtales.views.LogEditView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="@dimen/dialog_margin_top"
android:background="@color/background_material_light"
android:theme="@style/DialogTheme">
<include
android:id="@+id/app_bar"
layout="@layout/app_bar_edit" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/app_bar">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<Button
android:id="@+id/btnCatchDate"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:textAllCaps="false" />
<Button
android:id="@+id/btnCatchTime"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:textAllCaps="false" />
<EditText
android:id="@+id/editLatitudeLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@id/btnCatchDate"
android:ems="10"
android:hint="@string/label_latitude"
android:inputType="numberDecimal" />
<EditText
android:id="@+id/editLongitudeLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@id/editLatitudeLabel"
android:ems="10"
android:hint="@string/label_longitude"
android:inputType="numberDecimal" />
<ImageView
android:id="@+id/imgCatchPicture"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignTop="@id/editLatitudeLabel"
android:background="@android:color/darker_gray"
android:contentDescription="@string/imgcatchpicture_contentdescription"
android:scaleType="centerInside"
android:src="@android:drawable/ic_menu_camera" />
<Spinner
android:id="@+id/spinnerLocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@id/editLongitudeLabel" />
<TextView
android:id="@+id/txtFishHeader"
style="?android:attr/listSeparatorTextViewStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@id/spinnerLocation"
android:text="@string/section_fish" />
<Spinner
android:id="@+id/spinnerSpeciesType"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@id/txtFishHeader" />
<EditText
android:id="@+id/editLengthLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@id/spinnerSpeciesType"
android:ems="5"
android:hint="@string/hint_length"
android:inputType="numberDecimal" />
<EditText
android:id="@+id/editWeightLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="@id/spinnerSpeciesType"
android:ems="5"
android:hint="@string/hint_weight"
android:inputType="numberDecimal" />
<EditText
android:id="@+id/editLengthLabel2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@id/editLengthLabel"
android:ems="5"
android:hint="@string/hint_length"
android:inputType="numberDecimal" />
<EditText
android:id="@+id/editWeightLabel2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="@id/editWeightLabel"
android:ems="5"
android:hint="@string/hint_weight"
android:inputType="numberDecimal" />
</RelativeLayout>
</ScrollView>
</com.ahappydevelopment.android.fishtales.views.LogEditView>
。我甚至在布局中定义了ScrollView
。我已尝试在ScrollView上设置android:theme="@style/DialogTheme"
,正如我在另一个StackOverflow帖子中看到的那样。
在Java方面,我对这样定义的android:fillViewport="true"
方法进行了覆盖:
onCreateDialog
启动@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Dialog dialog = new Dialog(getActivity(), R.style.DialogTheme);
//set to adjust screen height automatically, when soft keyboard appears on screen
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
return dialog;
}
的{{1}}有一个如下所示的方法:
Fragment
正如我所说,这对KitKat仿真器工作正常。这不适用于Lollipop模拟器。您可以看到我正在努力让DialogFragment
进行调整大小调整。 :-)有没有人看到我错过或做错了什么?此外,如果有一些我忽略的代码可以帮助找到解决方案,请告诉我,我也会发布。感谢您提供的任何见解。
答案 0 :(得分:3)
在我发现这个答案之前花了一段时间。我实际上已将DialogFragment
重构为AppCompatActivity
。然后我开始认为,为Android Lollipop设置的导航抽屉也许应该归咎于我遇到DialogFragment
时遇到的问题。我的用于API级别21及更高级别的MainActivity布局文件使用DrawerLayout
窗口小部件,其中ScrimInsetsFrameLayout
用于导航抽屉视图。因此,DrawerLayout
和ScrimInsetsFrameLayout
在其定义中都使用android:fitsSystemWindows="true"
作为实现导航抽屉的Material Design UI规范的一部分。 android:fitsSystemWindows="true"
定义中的DrawerLayout
干扰了AndroidManifest.xml文件中为MainActivity定义的android:windowSoftInputMode="stateHidden|adjustResize"
,以便在启动软键盘时调整对话框并调整其大小。以下是MainActivity的XML布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<!-- The Main Content View -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/app_bar"
layout="@layout/app_bar_main" />
<FrameLayout
android:id="@+id/fragment_main_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/app_bar" />
</RelativeLayout>
<!-- The Navigation Drawer View -->
<com.ahappydevelopment.android.fishtales.widgets.ScrimInsetsFrameLayout
android:id="@+id/scrimInsetsFrameLayout"
android:layout_width="@dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
android:elevation="10dp"
android:fitsSystemWindows="true"
app:insetForeground="#4000">
<FrameLayout
android:id="@+id/fragment_drawer_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</com.ahappydevelopment.android.fishtales.widgets.ScrimInsetsFrameLayout>
</android.support.v4.widget.DrawerLayout>
告诉FragmentManager添加DialogFragment
时,我使用android.R.id.content
作为视图容器,这是文档规定的。通过试验使用哪个视图容器,我能够找到一个给出预期结果的视图。就我而言,它是MainActivity布局文件中定义的DrawerLayout
视图。
LogEditFragment addLog = LogEditFragment.get(0l);
if(mIsLargeLayout) {
// The device is using a large layout, so show the fragment as a dialog
addLog.show(getFragmentManager(), "AddLog");
} else {
// The device is smaller, so show the fragment fullscreen
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// For a little polish, specify a transition animation
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
// To make it fullscreen, use the 'drawer_layout' view
// as the container for the fragment
transaction.add(R.id.drawer_layout, addLog).addToBackStack(null).commit();
}
完成这些更改并重构我的LogEditFragment
类以再次扩展DialogFragment
后,对话框和导航抽屉的行为都符合预期。