我想更改默认的AlertDialog布局大小。
我创建了课程:
public class SampleDialog : MvxDialogFragment
{
public override Dialog OnCreateDialog(Bundle savedInstanceState)
{
var dialog = new AlertDialog.Builder(Context);
dialog.SetView(View.Inflate(Context,Resource.Layout.SampleDialog,null));
return dialog.Create();
}
public override void OnStart()
{
if (Dialog == null) { return; }
Dialog.Window.SetLayout(200,460);
base.OnStart();
}
}
它的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/text1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/text2" />
</LinearLayout>
我正在从片段视图创建对话框:
var dialog = new SampleDialog
{
ViewModel = ViewModel,
Cancelable = true
};
dialog.Show(FragmentManager, "");
尝试在OnCreateDialog方法,OnStart中设置布局宽度,或者直接通过设置LinearLayout.layout_width属性来设置布局宽度,但结果是相同的。
如何配置?
答案 0 :(得分:1)
充气布局
在对布局进行充气时,您可以使用默认的充气机或Mvvmcross BindingInflate
,它将应用您需要的任何xml绑定。
默认充气机:
public override Dialog OnCreateDialog(Bundle savedInstanceState)
{
base.EnsureBindingContextSet(savedInstanceState);
var dialog = new AlertDialog.Builder(Context);
dialog.SetView(Activity.LayoutInflater.Inflate(Resource.Layout.SampleDialog, null));
return dialog.Create();
}
Mvvmcross xml绑定充气程序:
public override Dialog OnCreateDialog(Bundle savedInstanceState)
{
base.EnsureBindingContextSet(savedInstanceState);
var dialog = new AlertDialog.Builder(Context);
dialog.SetView(this.BindingInflate(Resource.Layout.SampleDialog, null));
return dialog.Create();
}
调整布局
Dialog.Window.SetLayout()
接收200(宽度)和460(高度)的像素值,这些像素值的物理大小会因设备屏幕分辨率的不同而不同。
更好的方法是使用dp并将其转换为像素:
将所需尺寸标注添加到dimens.xml
<resources>
<dimen name="dialog_width">400dp</dimen>
<dimen name="dialog_height">200dp</dimen>
</resources>
更新SampleDialog
public override void OnStart()
{
base.OnStart();
var width = Resources.GetDimension(Resource.Dimension.dialog_width);
var height = Resources.GetDimension(Resource.Dimension.dialog_height);
Dialog.Window.SetLayout((int)width, (int)height);
}