2015-03-25 52 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,但我没有找到更好的解决方案。