2015-03-25 33 views
2

Default AlertDialog看起來不錯,使用setMessage()可以設置其文本 - 簡單。使用setView時的AlertDialog風格()

但是當使用setView()時,對話框會用給定視圖替換內容,這很好。

但是,我想保留默認的填充/背景/風格。我搜索了一個適當的樣式,我看了一下alert_dialog.xml,但沒有找到有用的東西。 enter image description here

如何應用默認樣式,而不模仿它?

+0

請發佈您的佈局代碼n警報對話框代碼 – Yogendra 2015-03-25 12:21:43

+1

您必須在視圖上手動設置邊距。我認爲他們左右都是棒棒堂24dp。 (與標題對齊)。 – 2015-03-25 12:25:11

+0

PLZ刪除所有樣式,手動給予適當的邊距。 – Yogendra 2015-03-25 13:02:29

回答

0

有一個AlertDialog風格,那您可以在創建Builder時進行設置。

例子:

AlertDialog.Builder builder; 
    if (title != null) { 
     builder = new AlertDialog.Builder(getActivity(), R.style.AlertDialogStyle); 
     builder.setTitle(title); 
    } 
    else { 
     builder = new AlertDialog.Builder(getActivity(), R.style.AlertDialogStyleNoTitle); 
    } 

在你styles.xml文件:

<style name="AlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert"> 
    <item name="colorPrimary">@color/primary</item> 
    <item name="colorPrimaryDark">@color/primary_dark</item> 
    <item name="colorAccent">@color/accent</item> 
    <item name="android:textColorPrimary">@color/black</item> 
</style> 

<style name="AlertDialogStyleNoTitle" parent="AlertDialogStyle"> 
    <item name="android:windowNoTitle">true</item> 
</style> 

我的風格是隻是一些事情你可以做一個例子 - 你可以有一個對話的風格,如果你想。只要你從Dialog.Alert風格之一繼承,你應該很好。

您仍然必須自行設置填充。我查看了材料設計指南,邊距是24dp。

0

用途:

AlertDialog.Builder b = new AlertDialog.Builder(this); 

AlertDialog alert = b.create(); 
alert.setView(view, 0, 0, 0, 0); 
+0

這將利潤率設置爲零,這不是我想要的。 – 2015-03-30 13:13:15

0

要解決,你可以使用包裝你的佈局在的FrameLayout的利潤,在佈局中添加的利潤率是這樣的:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginTop="@dimen/dialog_margin_title" 
     android:layout_marginBottom="@dimen/dialog_margin" 
     android:layout_marginLeft="@dimen/dialog_margin" 
     android:layout_marginStart="@dimen/dialog_margin" 
     android:layout_marginRight="@dimen/dialog_margin" 
     android:layout_marginEnd="@dimen/dialog_margin" 
     android:orientation="vertical"> 

     <TextView 
      android:id="@+id/textView" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Please enter the email address of the person you would like to follow, this person will be notified." /> 

     <EditText 
      android:id="@+id/editText" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:ems="10" 
      android:inputType="textEmailAddress" /> 
    </LinearLayout> 
</FrameLayout> 
0

基礎上程序兼容性的源代碼

https://chromium.googlesource.com/android_tools/+/HEAD/sdk/extras/android/support/v7/appcompat/res/layout/abc_alert_dialog_material.xml

這是很好看

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:paddingBottom="@dimen/abc_dialog_padding_top_material" 
    android:paddingLeft="?attr/dialogPreferredPadding" 
    android:paddingRight="?attr/dialogPreferredPadding" 
    android:paddingTop="@dimen/abc_dialog_padding_top_material"> 

    <Spinner 
     android:id="@+id/spinner1" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_marginRight="10dp" 
     android:layout_weight="1" /> 

    <Spinner 
     android:id="@+id/spinner2" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" /> 
</LinearLayout> 

注意,它不是raccomanded使用私人對話框自定義視圖佈局的實例值,例如abc_dialog_padding_top_material,但我沒有找到更好的解決方案。