2016-11-01 124 views
0

我想更改AlertDialog的默認佈局大小。如何更改MvxDialogFragment佈局

我創建的類:

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, ""); 

Tryed設置佈局寬度OnCreateDialog方法,OnStart,或直接通過設置LinearLayout.layout_width屬性,但結果是相同的。

這是怎麼配置的?

回答

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(hei ght),這將根據不同的設備屏幕分辨率在物理尺寸上有所不同。

一個更好的辦法是使用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); 
}