MyCustomDialogFragment总是包装它的内容,并且不像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");
}
这是MyCustomDialogFragment。
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>
结果样本:
答案 0 :(得分:2)
找到一个简单的解决方案,以保持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);
}
答案 1 :(得分:1)
试试这些方法
在代码中设置布局宽度和高度
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))
答案 2 :(得分:0)
我今天遇到了这个问题,我找到了一个简单的解决方案:您可以从getTheme()
覆盖DialogFragment
方法并从中返回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
}
}