簡體   English   中英

Android中的完全自定義對話框

[英]Fully Custom Dialog Box in Android

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:elevation="4dp"

    android:background="@color/colorWhite">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/add_album_title"
        android:shadowColor="@color/colorWhite"
        android:text="Enter the Album Name"
        android:textSize="20sp"
        />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/new_album_name"
        android:textSize="25sp"
        android:hint="Album name"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="2">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:id="@+id/add_new_album_button"
        android:text="Add"
        android:textAllCaps="false"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:id="@+id/cancel_new_album_button"
        android:text="Cancel"
        android:textAllCaps="false"/>
    </LinearLayout>
</LinearLayout>

我有這個代碼作為布局。 我想在我的Activity中將其設置為對話框。 但是當我運行它時看起來很簡單。 我是Android初學者,沒有太多的造型知識。 有人可以幫我設置對話框的樣式。

這是我實現它的代碼。 我剛剛對按鈕進行了評論,因為現在我的重點是造型並使其更具吸引力。

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

如果有人能提供幫助,我將非常感激。

如果要使用漂亮的外觀進行自定義對話,則需要設置對話框的樣式和主題。

樣式是一組屬性,用於指定視圖或窗口的外觀和格式。

我只是給你發一些樣式的例子,你可以通過下面的線把它設置到你的對話框:

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

style.xml

您需要在style.xml中創建新樣式:

<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>

這只是一個例子。 如果您想了解更多詳情,請點擊以下鏈接: http//blog.supenta.com/2014/07/02/how-to-style-alertdialogs-like-a-pro/

希望它會對你有所幫助。

   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();
    }

並在style.xml設置樣式:

 <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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM