2013-03-02 6 views
10

मैं कस्टम view (कोई शीर्षक या पाद लेख) और गोलाकार कोनों के साथ AlertDialog बनाने की कोशिश कर रहा हूं। मैंने ऐसा करने के बारे में बहुत सी पोस्ट देखी हैं और मैंने बहुत सी चीजों की कोशिश की, लेकिन मैं इसे बनाना नहीं चाहता जैसे मैं चाहता हूं।एंड्रॉइड: कस्टम व्यू और गोलाकार कोनों के साथ अलर्टडियलॉग

enter image description here

मैं dialog dialog_background.xml कहा जाता है के लिए एक drawable बनाया:

<shape xmlns:android="http://schemas.android.com/apk/res/android" > 

    <solid 
     android:color="#FFAAAAAA" /> 

    <stroke 
     android:width="2dp" 
     android:color="#FF000000" /> 

    <corners android:radius="20dp" /> 
</shape> 

और मैं एक style कहा कि यह उपयोग करने के लिए:

<style name="MyDialog" parent="@android:style/Theme.Dialog"> 
    <item name="android:background">@drawable/dialog_background</item> 
</style> 

यह मेरा लक्ष्य है

मेरे कस्टम view में दो बटन होंगे। अभी इसे आसान बनाने के लिए मैं आपको खाली LinearLayout दिखाता हूं।

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    style="@style/MyDialog" 
    > 

</LinearLayout> 

Dialog निर्माण करने के लिए मैं एक DialogFragment का उपयोग करें: यह playdialog.xml है। ,

public Dialog onCreateDialog(Bundle savedInstanceState) { 
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
    LayoutInflater inflater = getActivity().getLayoutInflater(); 

    builder.setView(inflater.inflate(R.layout.playdialog, null)); 
    return builder.create(); 
} 

ठीक है अगर मैं उस तरह कोड का उपयोग मैं इस मिल: यह अपने onCreateDialog समारोह है

enter image description here

मैं अपने DialogFragment कोड को संशोधित करने के लिए पारदर्शी dialog पृष्ठभूमि सेट करने की कोशिश की:

public Dialog onCreateDialog(Bundle savedInstanceState) { 
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
    LayoutInflater inflater = getActivity().getLayoutInflater(); 

    builder.setView(inflater.inflate(R.layout.playdialog, null)); 

    **NEW**   

    Dialog d = builder.create(); 
    d.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT)); 
    return d; 
} 

परिणाम बिल्कुल वही है, इसलिए मुझे एहसास हुआ कि वह सफेद आयताकार मेरे dialogमेरे कस्टम view से है, dialog से नहीं। मैं पहले से ही view की पृष्ठभूमि को dialog_background.xml पर सेट कर रहा हूं, इसलिए मैं इसे पारदर्शी पर भी सेट नहीं कर सकता हूं या मैंने कोनों, रंग इत्यादि को खोला है।

फिर मैंने dialog की पृष्ठभूमि को संशोधित करने का निर्णय लिया dialog_background.xml और मेरे दृश्य में पृष्ठभूमि के रूप में ठोस रंग है।

अपने कस्टम में viewlayout (playdialog.xml) मैं के साथ प्रतिस्थापित शैली = "@ शैली/MyDialog":

android:background="#FFAAAAAA" 

और फिर मेरी DialogFragment में मैं इस कोड का इस्तेमाल किया:

public Dialog onCreateDialog(Bundle savedInstanceState) { 
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
    LayoutInflater inflater = getActivity().getLayoutInflater(); 

    builder.setView(inflater.inflate(R.layout.playdialog, null)); 

    **NEW**   

    Dialog d = builder.create(); 
    d.getWindow().setBackgroundDrawableResource(R.drawable.dialog_background); 
    return d; 
} 

यह मुझे मिलता है:

enter image description here

यह लगभग वही है जो मैं चाहता था, लेकिन आप मेरी कस्टम view सीमाएं देख सकते हैं, इसलिए यह पर्याप्त नहीं है। इस बिंदु पर मुझे नहीं पता था कि मैं और क्या कर सकता हूं।

कोई भी जानता है कि मैं इसे कैसे हल कर सकता हूं?

धन्यवाद!

+0

क्या आप समस्या को ठीक कर सकते हैं? गोलाकार कोने के साथ, मैं – sabbir

उत्तर

5

मैं सिर्फ एक कस्टम अलर्ट संवाद बनाया। लेकिन इसके कोनों गोल नहीं हैं।

String message = "Your Message"; 
callAlert(message, callingClass.this); 
- के रूप में इस विधि

public static void callAlert(String message, final Context context){ 
    final Dialog dialog = new Dialog(context); 
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    dialog.setContentView(R.layout.customdialog_layout); 
    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.WHITE));    

    TextView tvTitle = (TextView) dialog.findViewById(R.id.textview_dialog_title); 
    tvTitle.setText("MyApp.."); 

    TextView tvText = (TextView) dialog.findViewById(R.id.textview_dialog_text); 
    tvText.setText(message);  

    Button buttonDialogYes = (Button) dialog.findViewById(R.id.button_dialog_yes); 
    buttonDialogYes.setOnClickListener(new OnClickListener() {   
     public void onClick(View v) { 
      // Do your stuff... 
      dialog.dismiss(); 
     } 
    }); 

    Button buttonDialogNo = (Button) dialog.findViewById(R.id.button_dialog_no); 
    buttonDialogNo.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      // Do your stuff... 
      dialog.dismiss(); 
     }   
    }); 
    dialog.show(); 
} 

और कहते हैं -

पहले के रूप में यह के लिए एक लेआउट बना सकते हैं -

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="240sp" 
android:layout_height="wrap_content" 
android:background="#FFFFFF" 
tools:ignore="SelectableText" > 

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="1sp" 
    tools:ignore="UselessParent" > 

    <TableLayout 
     android:id="@+id/tablelayout_dialog_title" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:stretchColumns="1" > 

     <TableRow 
      android:id="@+id/tablerow_dialog_title" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"    
      tools:ignore="UselessParent" > 

      <ImageView 
       android:id="@+id/imageview_dialog_image" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:src="@drawable/alertwhite" 
       android:layout_gravity="center" 
       android:background="#643c3a" 
       android:contentDescription="@string/string_todo"/> 

      <TextView 
       android:id="@+id/textview_dialog_title" 
       android:layout_width="wrap_content" 
       android:layout_height="fill_parent" 
       android:background="#643c3a" 
       android:padding="10sp" 
       android:gravity="center_vertical" 
       android:textColor="#FFFFFF" 
       android:textSize="15sp" /> 

     </TableRow>  
    </TableLayout> 

    <View 
     android:id="@+id/viewline_dialog" 
     android:layout_below="@+id/tablelayout_dialog_title" 
     android:layout_width = "wrap_content" 
     android:layout_height="0.25dip" 
     android:background="#ffffff"   
     android:layout_centerVertical ="true" /> 

    <TextView 
     android:id="@+id/textview_dialog_text" 
     android:layout_below="@+id/viewline_dialog" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:padding="8sp" 
     android:background="#643c3a" 
     android:textColor="#FFFFFF" 
     android:textSize="12sp" /> 

    <View 
     android:id="@+id/viewline1_dialog" 
     android:layout_width = "wrap_content" 
     android:layout_height="0.5dip" 
     android:background="#ffffff"   
     android:layout_centerVertical ="true" 
     android:layout_below="@+id/textview_dialog_text"/> 

    <TableLayout 
     android:id="@+id/tablelayout_dialog_button" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:stretchColumns="*" 
     android:layout_below="@+id/viewline1_dialog" 
     android:background="#a8a8a8" > 

     <TableRow 
      android:id="@+id/tablerow_dialog_button" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"    
      tools:ignore="UselessParent" > 

      <Button 
       android:id="@+id/button_dialog_yes" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_margin="8sp" 
       android:paddingTop="5sp" 
       android:paddingBottom="5sp" 
       android:background="@drawable/roundedcornerbuttonfordialog_shape" 
       android:text="@string/string_yes" /> 

      <Button 
       android:id="@+id/button_dialog_no" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_margin="8sp" 
       android:paddingTop="5sp" 
       android:paddingBottom="5sp" 
       android:background="@drawable/roundedcornerbuttonfordialog_shape" 
       android:text="@string/string_no" /> 

     </TableRow> 
    </TableLayout> 

</RelativeLayout> 

अब के रूप में संवाद का कोड लिखने

आशा है कि यह हेल होगा पी आप

+1

अलर्टडियलोग की बजाय एक संवाद का उपयोग करना, जैसा आपने किया था, समस्या हल हो गई। धन्यवाद! : डी –

+0

आईएमओ, मैं स्वरूपण करने और करने के लिए टेबललाउट का उपयोग नहीं करता। मैं रैखिक, सापेक्ष, और फ्रेम के साथ रहना होगा। अपने दृश्य पदानुक्रम प्रदर्शन को बढ़ाकर प्रदर्शन बढ़ाएं। – jpotts18

-1

आप जिस छवि को दिखा रहे हैं उसका उपयोग क्यों नहीं करते हैं "यह मेरा लक्ष्य है:" अपने लक्ष्य बीजी छवि को खींचने योग्य फ़ोल्डर में सहेजें।

private AlertDialog createAlertDialog() { 
    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setView(getLayoutInflater().inflate(R.layout.playdialog, null)); 
    return builder.create(); 
} 

तो

AlertDialog dialog = createAlertDialog(); 
dialog.show(); 
+0

में सफेद वर्गों को खत्म करने में सक्षम नहीं हूं, स्केलेबिलिटी, और कम्प्यूटेशनल री-रेंडरिंग समय और इसके लिए अनावश्यक बिजली खपत जैसे कारणों के लिए छवियों का उपयोग अनुशंसित नहीं है। – computingfreak

1

मेरे मामले में, संवाद में सीमाओं को दूर करने के लिए इस समाधान मददगार थे:

dialog.setStyle(DialogFragment.STYLE_NO_FRAME, 0); 

यहाँ वर्णित https://stackoverflow.com/a/19521674/3173384

0

नीचे कोड मुद्दा

MyDialog mydialog = new MyDialog(this, "for testing", 
      new myOnClickListener() { 

       @Override 
       public void onPositiveButtonClick() { 
        // TODO Auto-generated method stub 
        Toast.makeText(getApplicationContext(), 
          "I am positive button in the dialog", 
          Toast.LENGTH_LONG).show(); 
       } 

       @Override 
       public void onNegativeButtonClick() { 
        // TODO Auto-generated method stub 
        Toast.makeText(getApplicationContext(), 
          "I am negative button in the dialog", 
          Toast.LENGTH_LONG).show(); 
       } 
      }); 

    // this will remove rectangle frame around the Dialog 
    mydialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent); 
    mydialog.show(); 

धन्यवाद, नागेन्द्र हल की तरह

+0

यह सीमा में सफेद वर्गों को खत्म नहीं किया :( – sabbir

संबंधित मुद्दे