Android中的完全自定义对话框

Android中的完全自定义对话框,android,android-layout,dialog,android-dialogfragment,Android,Android Layout,Dialog,Android Dialogfragment,在主要活动中,我使用此代码来显示它 public class AddAlbumDialog extends DialogFragment { Button addButton,cancelButton; EditText newAlbumName; TextView title; Context context; public AddAlbumDialog() { //Empty Constructor } @Ove

在主要活动中,我使用此代码来显示它

public class AddAlbumDialog extends DialogFragment {
    Button addButton,cancelButton;
    EditText newAlbumName;
    TextView title;
    Context context;

    public AddAlbumDialog() {
        //Empty Constructor
    }



    @Override
    public void onStart() {
        super.onStart();

        Dialog dialog = getDialog();
        if (dialog != null) {
            dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            dialog.getWindow().setGravity(Gravity.CENTER);
        }
    }


    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        // Get the layout inflater
        LayoutInflater inflater = getActivity().getLayoutInflater();
        builder.setCancelable(false);


        // Inflate and set the layout for the dialog
        // Pass null as the parent view because its going in the dialog layout
        builder.setView(inflater.inflate(R.layout.add_album_dialog_fragment,null));
                // Add action buttons
//        .setPositiveButton(R.string.add_album, null)
//            @Override
//            public void onClick(DialogInterface dialog, int which) {
//                Dialog d = (Dialog) dialog;
//
//                EditText newAddAlbum = (EditText) d.findViewById(R.id.new_album_name);
//                newAddAlbum.requestFocus();
//                if (newAddAlbum.getText().toString().trim().isEmpty()) {
//                    Toast.makeText(getActivity(), "Empty Name Cannot Add", Toast.LENGTH_SHORT).show();
//
//                } else {
//                    mListener.onDialogPositiveClick(AddAlbumDialog.this, newAddAlbum);
//                }
//            }
//        })
                //Add Negative Button
//        .setNegativeButton(R.string.cancel_dialog, new DialogInterface.OnClickListener() {
//            @Override
//            public void onClick(DialogInterface dialog, int which) {
//                Toast.makeText(getActivity(), "Cancel Called Name", Toast.LENGTH_SHORT).show();
//                mListener.onDialogNegativeClick(AddAlbumDialog.this);
//                dismiss();
//            }
//        });




        return builder.create();
    }









    /* The activity that creates an instance of this dialog fragment must
    * implement this interface in order to receive event callbacks.
    * Each method passes the DialogFragment in case the host needs to query it. */
    public interface AddAdlbumListener {
        public void onDialogPositiveClick(DialogFragment dialog,EditText newAlbumName);
        public void onDialogNegativeClick(DialogFragment dialog);
    }

    // Use this instance of the interface to deliver action events
    AddAdlbumListener mListener;

    // Override the Fragment.onAttach() method to instantiate the NoticeDialogListener
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        // Verify that the host activity implements the callback interface
        try {
            // Instantiate the NoticeDialogListener so we can send events to the host
            mListener = (AddAdlbumListener) activity;
        } catch (ClassCastException e) {
            // The activity doesn't implement the interface, throw exception
            throw new ClassCastException(activity.toString() + " must implement NoticeDialogListener");
        }
    }




}
如果有人能帮忙,我将非常感激

AddAlbumDialog addAlbumDialog=new AddAlbumDialog();


addAlbumDialog.show(getSupportFragmentManager(),"Custom Dialog");
并在
style.xml
中设置样式:

   public void dialog()
    {
        final Dialog dialog = new Dialog(HomeActivity.this, R.style.cust_dialog);
        dialog.setTitle("Select Content Language");

        dialog.setContentView(R.layout.add_album_dialog_fragment);
      //  dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        dialog.setCanceledOnTouchOutside(false);
        dialog.setCancelable(false);

        Button  button=(Button)dialog.findViewById(R.id.textView_camera);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialogLanguage();
                ll_slide.setVisibility(View.GONE);

                dialog.dismiss();
            }
        });

        dialog.show();
    }

@样式/对话框\u标题\u样式
@颜色/状态栏
@android:彩色/透明
@颜色/白色
20便士
10dp
水平中心|垂直中心|

若要使“自定义”对话框外观美观,则需要设置对话框的样式和主题

是指定视图或窗口的外观和格式的属性集合

我刚刚向您发布了一些样式示例,您可以通过以下行将其设置为对话框:

 <style name="cust_dialog" parent="@android:style/Theme.Dialog">
        <item name="android:windowTitleStyle">@style/dialog_title_style</item>

    </style>
  <style name="dialog_title_style" parent="android:Widget.TextView">
        <item name="android:background">@color/status_bar</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:textColor">@color/white</item>
        <item name="android:textSize">20sp</item>
        <item name="android:padding">10dp</item>
        <item name="android:gravity">center_horizontal|center_vertical</item>
    </style>
style.xml

您需要在style.xml中创建新样式:

Dialog dialog = new Dialog(YourActivity.this, R.style.MyTheme);

@颜色/白色
@颜色/白色
@颜色/白色
@颜色/白色
@颜色/白色
@颜色/白色
@颜色/橙色
@颜色/橙色
@颜色/蓝色
@颜色/蓝色
@样式/自定义对话框主题
这只是一个例子。如果您想了解更多详细信息,请访问以下链接:


希望它能对您有所帮助。

请关注感谢Kishu,这是一个非常有帮助的快速回复。感谢Android极客,感谢您的快速回复。谢谢你。@AnandVardhan很好……如果这有助于你对答案投赞成票……提前谢谢:)
Dialog dialog = new Dialog(YourActivity.this, R.style.MyTheme);
<style name="CustomDialogTheme" parent="@android:style/Theme.Dialog">
    <item name="android:bottomBright">@color/white</item>
    <item name="android:bottomDark">@color/white</item>
    <item name="android:bottomMedium">@color/white</item>
    <item name="android:centerBright">@color/white</item>
    <item name="android:centerDark">@color/white</item>
    <item name="android:centerMedium">@color/white</item>
    <item name="android:fullBright">@color/orange</item>
    <item name="android:fullDark">@color/orange</item>
    <item name="android:topBright">@color/blue</item>
    <item name="android:topDark">@color/blue</item>
</style>

<style name="MyTheme">
    <item name="android:alertDialogStyle">@style/CustomDialogTheme</item>
</style>