android DialogFragment始终 Package 其内容,并且不保留警报对话框的最小宽度

sd2nnvve  于 2023-03-16  发布在  Android
关注(0)|答案(4)|浏览(133)

MyCustomDialogFragment总是 Package 它的内容,并且不像Alertdialog那样保持最小宽度。我如何设置宽度以作为标准Android对话框响应?
下面是我调用来显示对话框的函数:

private void displayDialogEdit(String title, String edit,
MyCustomEditDialog.MyCustomEditDialogListener listener, boolean onlyNumbers){
            MyCustomEditDialog dialog = MyCustomEditDialog.newInstance(title, edit, onlyNumbers);
            dialog.setInterface(listener);
            dialog.show(getSupportFragmentManager(), "edit");
        }

这是我的自定义对话框片段。

public class MyCustomEditDialog extends DialogFragment {

    private MyCustomEditDialog.MyCustomEditDialogListener mInterface;

    public static MyCustomEditDialog newInstance(String title, String editText, boolean onlyNumbers){
        MyCustomEditDialog dialog = new MyCustomEditDialog();
        Bundle args = new Bundle();
        args.putString("title", title);
        args.putString("edit", editText);
        args.putBoolean("numbers", onlyNumbers);
        dialog.setArguments(args);
        return dialog;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_edit_dialog, container);

        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);

//        Bundle args = getArguments();
//
//        boolean numbers = args.getBoolean("numbers");
//
//        TextView txt = (TextView)v.findViewById(R.id.title);
//        txt.setText(args.getString("title"));
//
//        final EditText edit = (EditText) v.findViewById(R.id.editText);
//        edit.setText(args.getString("edit"));
//
//        if (numbers) edit.setInputType(InputType.TYPE_CLASS_NUMBER);
//
//        Button btn = (Button)v.findViewById(R.id.save);
//
//        btn.setOnClickListener(new View.OnClickListener() {
//            @Override public void onClick(View view) {
//                if (mInterface!=null) mInterface.customDialogOnClick(edit.getText().toString());
//            }
//        });
//
//        setCancelable(true);

        return v;
    }

    public void setInterface(MyCustomEditDialog.MyCustomEditDialogListener listener){
        this.mInterface = listener;
    }

    public interface MyCustomEditDialogListener {
        void customDialogOnClick(String result);
    }

}

这是我的自定义对话框布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingStart="@dimen/activity_horizontal_margin"
        android:paddingEnd="@dimen/activity_horizontal_margin"
        android:gravity="center_horizontal"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:textColor="@color/blue_dark"
        android:textStyle="bold"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:layout_marginBottom="10dp"
        android:id="@+id/title"
        android:layout_gravity="center_horizontal" />

    <EditText
        android:layout_marginStart="@dimen/activity_horizontal_margin"
        android:layout_marginEnd="@dimen/activity_horizontal_margin"
        android:layout_marginBottom="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/editText"
        android:inputType="text"
        />

    <Button
        style="@style/btnBasic.blue"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/save"
        android:id="@+id/save" />

</LinearLayout>

结果样本:

f0ofjuux

f0ofjuux1#

找到了一个简单的解决方案来保持fragmentDialog上alertDialog的标准。
使用以下代码重写片段中的onCreate方法:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(DialogFragment.STYLE_NO_TITLE, android.R.style.Theme_Material_Light_Dialog_Alert);
}
ki0zmccv

ki0zmccv2#

试试这些方法
*在代码中设置布局宽度和高度

alertDialog.getWindow().setLayout(600, 400);

*设置对话框的主主题

android:theme="@android:style/Theme.Dialog"

主题

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:dialogTheme">@style/dialog_light</item>


</style>

<style name="dialog_light" parent="@android:style/Theme.Dialog">
<item name="@android:windowBackground">@color/whitegrey</item>
<item name="@android:textColor">@color/black</item>
</style>

*在代码中设置主题也像这样

new AlertDialog.Builder(
new ContextThemeWrapper(context, android.R.style.Theme_Dialog))
jgzswidk

jgzswidk3#

我今天遇到了这个问题,我找到了一个简单的解决方案:您可以覆盖DialogFragment中的getTheme()方法,并从中返回R.style.Theme_AppCompat_Light_Dialog_Alert(或者您想要的任何其他主题,我使用了AppCompat主题,因此这将在旧版本的Android中工作)。
Kotlin中的示例代码:

class ExampleDialog : DialogFragment() {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.example_dialog, container, false)
    }

    override fun getTheme(): Int {
        return R.style.Theme_AppCompat_Light_Dialog_Alert
    }

}
gblwokeq

gblwokeq4#

编写此代码:对话框?.窗口?.应用{设置布局(线性布局.布局参数.MATCH_PARENT,线性布局.布局参数.MATCH_PARENT)}

相关问题