2016-04-02 7 views
1
<?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> 

Ich habe diesen Code als Layout. Ich möchte dies als einen Dialog in meiner Aktivität festlegen. Aber wenn ich es laufe, sieht einfach so einfach aus. Ich bin Anfänger in Android mit nicht viel Styling-Wissen. Kann mir bitte jemand helfen, die Dialogbox zu gestalten.Vollständig benutzerdefinierte Dialogfeld in Android

Hier ist mein Code für die Implementierung. Ich habe gerade die Buttons kommentiert, weil ich mich gerade auf Styling und Attraktivität fokussiere.

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




} 

Und in der Haupttätigkeit verwende ich diesen Code, um es zu zeigen.

AddAlbumDialog addAlbumDialog=new AddAlbumDialog(); 


addAlbumDialog.show(getSupportFragmentManager(),"Custom Dialog"); 

Ich werde sehr dankbar sein, wenn jemand helfen könnte.

+0

Folgen Sie http://www.mkyong.com/android/android-custom-dialog-example/ –

Antwort

2

Wenn Sie anpassen Dialog mit dem guten Blick machen wollen, dann müssen Sie Stil und Thema des Dialogs setzen.

Eine style ist eine Sammlung von Eigenschaften, die das Aussehen und Format für eine Ansicht oder ein Fenster angeben.

ich gerade bin Entsendung Ihnen einig Beispiels für Stil, die Sie es zu einem Dialog von unten Linie festlegen:

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

style.xml

Sie brauchen neuen Stil in der Art zu schaffen, .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> 

Dies ist nur ein Beispiel. Wenn Sie weitere Informationen erhalten möchten, folgen Sie bitte dem folgenden Link: http://blog.supenta.com/2014/07/02/how-to-style-alertdialogs-like-a-pro/

Ich hoffe, es wird Ihnen helfen.

+0

Vielen Dank Kishu, sehr hilfreich und schnelle Antwort. –

+0

@AnandVardhan: Ihre Begrüßung. – KishuDroid

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

und setzte Stil in 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> 
+0

Dank Android Geek, für Ihre schnelle und hilfreiche Antwort. Danke. –

+0

@AnandVardhan Großartig ... wenn dies geholfen hat, können Sie für die Antwort upvote .... Vielen Dank im Voraus :) –