android - 显示键盘时 DialogFragment 未调整大小

标签 android actionbarsherlock android-view android-dialogfragment

我正在尝试使用 SherlockDialogFragment 来询问用户的一些输入。在我的手机(Galaxy Nexus,4.2)上一切正常,但在较小的手机(模拟器 2.3.3)上,当键盘出现时,它会覆盖 DialogFragment 的两个按钮,如下所示:

dialog covered by keyboard

我的布局在 ScrollView 内,我将 onViewCreated 上的 softInputMode 更改为 SOFT_INPUT_ADJUST_RESIZE。我也试过 SOFT_INPUT_ADJUST_PAN,但没用

MyCustomDialog.java

public class AddTaskDialog extends SherlockDialogFragment implements OnDateSetListener{
//...
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
    }
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the Builder class for convenient dialog construction
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        this.inflater =  getActivity().getLayoutInflater();
        View mainView =inflater.inflate(R.layout.custom_dialog, null);
        builder.setView(mainView);
        this.taskNote = (EditText) mainView.findViewById(R.id.ET_taskNote);
        this.taskText = (EditText) mainView.findViewById(R.id.ET_taskText);
        this.taskValue = (EditText) mainView.findViewById(R.id.ET_taskValue);
        /*
         * Other stuff
         */
        builder.setTitle(getString(R.string.new_task, hType.toString()))
               .setPositiveButton(R.string.dialog_confirm_button, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                    //...
                    }
               })
               .setNegativeButton(R.string.dialog_cancel_button, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // User cancelled the dialog
                   }
               });
        // Create the AlertDialog object and return it
        return builder.create();
    }
}

这是我的布局:

custom_dialog.xml

<LinearLayout 
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" 
android:background="@color/abs__background_holo_light">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:paddingLeft="@dimen/activity_vertical_margin"
        android:paddingRight="@dimen/activity_vertical_margin">
        <TextView
            android:id="@+id/TV_taskText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/task_text"
            android:textAppearance="?android:attr/textAppearanceLarge" />
        <EditText
            android:id="@+id/ET_taskText"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:hint="@string/create_task_hint"
            android:inputType="textNoSuggestions"
            android:singleLine="true" />

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="@dimen/activity_vertical_margin"
        android:paddingRight="@dimen/activity_vertical_margin" >
        <TextView
            android:id="@+id/TV_taskNote"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/task_note"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <EditText
            android:id="@+id/ET_taskNote"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:minLines="2"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="textMultiLine"
            android:hint="@string/task_note_hint">

        </EditText>

    </LinearLayout>
    <LinearLayout
        android:id="@+id/repeat_days"
        android:layout_width="wrap_content"
        android:layout_height="48dp"
        android:layout_gravity="top"
        android:orientation="horizontal"
        android:visibility="gone"
        android:paddingLeft="@dimen/activity_vertical_margin"
        android:paddingRight="@dimen/activity_vertical_margin">
        <!-- Day buttons are put here programatically -->
    </LinearLayout>
</LinearLayout>

那么,您能帮我,并指导我如何显示这些按钮吗?要么平移 View ,要么让它调整大小...

最佳答案

我只是在 DialogFragment 中使用以下行:

getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

仅此而已,请参见此处的完整示例:

    public class TextEditor extends DialogFragment {

    public TextEditor () {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_text_editor, container);

        //set to adjust screen height automatically, when soft keyboard appears on screen 
        getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

        //[add more custom code...]
        return view;
    }
}

关于android - 显示键盘时 DialogFragment 未调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16885300/

相关文章:

android - ActionBarSherlock 错误膨胀类

android - 在 Android 的 Scrollview 中使用约束布局是个好主意吗?

android - 为什么 Glass 缺少 Google Play 服务?

android - 单击链接时未启动 InstantApp

java - Android - 将一个项目从一个ListView添加到另一个ListView?

java - NoClassDefFoundError : com. actionbarsherlock.app.SherlockFragmentActivity 版本 4

android - 在 Android 中通过蓝牙传输图像

android - listview 作为 sherlock 操作栏中的操作溢出

android - 通过 inflate 从合并布局到 RelativeLayout 的 XML 属性

android - 某些 Android 设备中未显示线性渐变