是否可以替换AlertDialog布局?

时间:2018-06-08 11:07:58

标签: android layout dialog

我一直在尝试创建自定义的AlertDialog,我知道我可以使用AlertDialog.Builder.setView()在对话框中放置自定义视图,但是可以完全替换默认布局吗?

编辑: 我想这样做是因为它允许我使用构建器setMessage()setTitle()等自定义布局

3 个答案:

答案 0 :(得分:2)

可以自定义或完全更改DialogAlertDialog,您可以像这样自定义Dialog

private void customDialog() {
        final Dialog dialog = new Dialog(ActivityUserVideoPlay.this, R.style.MaterialDialogSheet);
        dialog.setContentView(R.layout.your_layout_foor_dialog); // your custom view.
        dialog.setCancelable(false);
        dialog.getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

        dialog.show();
    }

这是对话框的样式

 <style name="MaterialDialogSheet" parent="@android:style/Theme.Dialog">
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:windowIsFloating">false</item>
        <item name="android:windowAnimationStyle">@style/MaterialDialogSheetAnimation</item>
    </style>

使用动画打开或关闭对话框,否则可以将其删除。

希望它有所帮助。

答案 1 :(得分:0)

是的,您可以,您只需要创建.xml并对其进行充气。就像那样:

final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        // Get the layout inflater
        LayoutInflater inflater = this.getLayoutInflater();
        View alertView = inflater.inflate(R.layout.newsletter_dialog, null);

        // Pass null as the parent view because its going in the dialog layout
        builder.setView(alertView);
        final AlertDialog dialog = builder.show();

答案 2 :(得分:0)

是的,您可以通过创建和扩充自己的视图来实现这一目标。

创建自定义视图文件,例如custom.xml

<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<ImageView
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="5dp" />

<TextView
    android:id="@+id/text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="#FFF" 
    android:layout_toRightOf="@+id/image"/>/>

 <Button
    android:id="@+id/dialogButtonOK"
    android:layout_width="100px"
    android:layout_height="wrap_content"
    android:text=" Ok "
    android:layout_marginTop="5dp"
    android:layout_marginRight="5dp"
    android:layout_below="@+id/image"
    />

  </RelativeLayout>

然后在java中:

   AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
  // ...Irrelevant code for customizing the buttons and title
  LayoutInflater inflater = this.getLayoutInflater();
   View dialogView = inflater.inflate(R.layout.custom, null);
  dialogBuilder.setView(dialogView);
   TextView text = (TextView) dialog.findViewById(R.id.text);
        text.setText("Android custom dialog example!");
        ImageView image = (ImageView) dialog.findViewById(R.id.image);
        image.setImageResource(R.drawable.ic_launcher);

        Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
        // if button is clicked, close the custom dialog
        dialogButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

 AlertDialog alertDialog = dialogBuilder.create();

  alertDialog.show();