2011-05-27 13 views
35

क्या कोई जानता है कि मैं एक कस्टम संवाद में मार्जिन कैसे सेट कर सकता हूं? मैं पूछ रहा हूं क्योंकि मेरे पास एक कस्टम संवाद है लेकिन जब प्रदर्शित होता है तो यह अभिभावक को भरने के लिए फैलता है, भले ही मैं लेआउट पैराम्स पर स्पष्ट रूप से WRAP_CONTENT सेट करता हूं।एक कस्टम संवाद में मार्जिन कैसे सेट करें?

असल में, संवाद में एक सूचीदृश्य होता है जिसका तत्व नीचे स्क्रॉल किया जाना चाहिए, जब तत्व उदाहरण के लिए 1 होते हैं, तो यह खिंचाव नहीं होता है, लेकिन जब अधिक आइटम जोड़े जाते हैं, तो संवाद पूरी स्क्रीन पर कब्जा कर लेता है।

कोई सुझाव? मैं संतोषजनक परिणाम प्राप्त करने

संपादित बिना संभव समाधान के सभी संभव संयोजनों की कोशिश कर रहा है: संवाद लेआउट

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="50dip" 
    android:orientation="vertical" 
    android:layout_gravity="top|center"> 

    <FrameLayout android:layout_width="fill_parent" android:layout_margin="5dip" android:layout_height="wrap_content"> 

      <TextView android:layout_width="wrap_content"  android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:textSize="20sp" android:textColor="@color/black"/> 

      <Button android:layout_height="32dip" android:layout_width="32dip" 
       android:id="@+id/guide_dialog_cross_button" 
       android:background="@drawable/button_cross_white"/> 

     </FrameLayout> 


    <ListView android:layout_width="fill_parent" android:layout_height="wrap_content" 
     android:fadingEdge="none" 
     android:layout_margin="5dip"/> 

    <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" 
     android:layout_margin="5dip" /> 

</LinearLayout> 
जोड़ा

उत्तर

71

मार्जिन संवाद के लिए काम नहीं करते, मैं कल्पना उच्च-स्तरीय विंडो दृश्य नहीं है मार्जिन का समर्थन करने वाला एक लेआउट प्रकार। मैंने पोस्ट्स को देखा है कि संवाद की शैली के रूप में परिभाषित होने पर मार्जिन काम करेगा (शीर्ष-स्तरीय दृश्य तत्व के बजाए), लेकिन ऐसा लगता है कि यह काम नहीं करता है।

इस समस्या के आसपास काम करने के लिए आपको क्या करना है inset का उपयोग अपनी संवाद पृष्ठभूमि के लिए करने योग्य है, और पृष्ठभूमि के अतिरिक्त इंसेट के लिए खाते में किसी भी पैडिंग को समायोजित करना है। नीचे दिए गए उदाहरण में, मैं बस & दाएं मार्जिन सेट कर दूंगा।

संवाद पृष्ठभूमि drawable:

<?xml version="1.0" encoding="utf-8"?> 
<!-- drawable is a reference to your 'real' dialog background --> 
<!-- insetRight and insetLeft add the margins --> 
<inset 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:drawable="@drawable/dialog_background" 
    android:insetRight="10dp" 
    android:insetLeft="10dp"> 
</inset> 

संवाद मुख्य दृश्य:

<?xml version="1.0" encoding="utf-8"?> 
<!-- paddingTop/paddingBottom padding for the dialog --> 
<!-- paddingLeft/paddingRight padding must add the additional inset space to be consistent --> 
<!-- background references the inset background drawable --> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:paddingTop="5dp" 
    android:paddingBottom="5dp" 
    android:paddingLeft="15dp" 
    android:paddingRight="15dp" 
    android:background="@drawable/dialog_background_inset"> 

<!-- ...the rest of the layout... --> 

तुम भी संवाद ही पारदर्शी करने के लिए की पृष्ठभूमि का रंग निर्धारित करने की आवश्यकता हो सकती है। इसलिए की तरह एक रंग संसाधन जोड़ें:

<color name="transparent">#00000000</color> 

और यह करने के लिए संवाद की खिड़की पृष्ठभूमि रंग सेट (ध्यान दें: आप सीधे रंग असाइन नहीं कर सकते, ग्रहण शिकायत)

<style name="Dialog" parent="android:style/Theme.Dialog"> 
    <item name="android:windowBackground">@color/transparent</item> 
    <item name="android:windowNoTitle">true</item> 
    <item name="android:windowIsFloating">true</item> 
</style> 

इस शैली में new Dialog(context, R.style.Dialog);

+2

आप संवाद शैली में windowBackground रूप dialog_background_inset drawable का उपयोग और संवाद के लिए एक पृष्ठभूमि का उपयोग नहीं करते लेआउट इनसेट के लिए अनुमति देने के लिए लेआउट में अतिरिक्त गद्दी सहित के बारे में चिंता नहीं है, तो। – ChrisJD

+3

@ChrisJD मैं इसे काम नहीं कर सका। वैसे भी, पोस्सी समाधान काम कर रहा है। कभी-कभी यह वास्तव में अजीब है कि कितना अजीब कोड एंड्रॉइड को बहुत सरल चीजों को लागू करने की आवश्यकता है। – Piotr

+1

@pospi क्या आप जानते हैं कि डायलॉग की सामग्री के बाहर क्लिक करते समय इसे रद्द करने योग्य कैसे बनाया जाए? क्योंकि यह आपके समाधान का उपयोग कर काम नहीं कर रहा है। – Piotr

10

मार्जिन does not को संवाद के लिए कस्टम लेआउट पर काम करने लगते हैं, लेकिन गद्दी के रूप में काम करता है theme तर्क के रूप में अपने संवाद के निर्माता को पारित किया जाना चाहिए। शीर्ष स्तर रैखिक लेआउट पर पैडिंग सेट करने का प्रयास करें।

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingLeft="4dp" 
    android:paddingRight="4dp" > 
5

का संभावित हल इस तरह किया जा सकता है:

dialog.getWindow().getAttributes().height = 
      (int) (getDeviceMetrics(context).heightPixels*0.8); 

`getDeviceMetrics विधि:

public static DisplayMetrics getDeviceMetrics(Context context) { 
    DisplayMetrics metrics = new DisplayMetrics(); 
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); 
    Display display = wm.getDefaultDisplay(); 
    display.getMetrics(metrics); 
    return metrics; 
} 
+0

यह केवल मेरे लिए संवाद के बाद इसे स्थापित करके काम करता है। show() ;, इस तरह: WindowManager LayoutParams params = dialog.getWindow()। GetAttributes(); params.height = (int) (Utils.getDeviceMetrics (LoginActivity.this) .height * 0.8); dialog.getWindow()। SetAttributes (पैराम्स); –

-1

एक सरल समाधान है कि मेरे लिए काम किया किसी अन्य रूप में अपने पूरे दृश्य रैप करने के लिए था एक, और मार्जिन सेट करें जिसमें अब आंतरिक दृश्य है।

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 

    <LinearLayout 
     android:layout_margin="22dp" 
     android:orientation="vertical" 
     android:layout_width="220dp" 
     android:layout_height="wrap_content"> 

    ... Buttons, TextViews, etc 

    </LinearLayout> 

</LinearLayout> 

Obs: कि काम किया जब मैं मेरी AlertDialog भीतर मार्जिन चाहता था। स्क्रीन के बारे में नहीं।

+0

तो आप इसे किसी अन्य लेआउट के अंदर लपेटने के बजाय पैडिंग जोड़ सकते थे। – arekolek

+0

रूट व्यू पर पैडिंग स्वयं काम नहीं कर सका। कृपया इसे मानने से पहले कोड का परीक्षण करें। यह गलत है और इसे कम कर रहा है। –

1

मार्जिन के साथ मेरे संवाद की स्थिति और आकार निर्धारित करने के लिए यह एक सीधा-आगे, प्रोग्रामेटिक तरीका है।

public static Dialog setMargins(Dialog dialog, int marginLeft, int marginTop, int marginRight, int marginBottom) 
{ 
    Window window = dialog.getWindow(); 
    if (window == null) 
    { 
     // dialog window is not available, cannot apply margins 
     return dialog; 
    } 
    Context context = dialog.getContext(); 

    // set dialog to fullscreen 
    RelativeLayout root = new RelativeLayout(context); 
    root.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); 
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    dialog.setContentView(root); 
    // set background to get rid of additional margins 
    window.setBackgroundDrawable(new ColorDrawable(Color.WHITE)); 

    // apply left and top margin directly 
    window.setGravity(Gravity.LEFT | Gravity.TOP); 
    LayoutParams attributes = window.getAttributes(); 
    attributes.x = marginLeft; 
    attributes.y = marginTop; 
    window.setAttributes(attributes); 

    // set right and bottom margin implicitly by calculating width and height of dialog 
    Point displaySize = getDisplayDimensions(context); 
    int width = displaySize.x - marginLeft - marginRight; 
    int height = displaySize.y - marginTop - marginBottom; 
    window.setLayout(width, height); 

    return dialog; 
} 

यहाँ सहायक तरीकों मैं कर रहे हैं:

public Dialog onCreateDialog(Bundle savedInstanceState) 
{ 
    // create dialog in an arbitrary way 
    Dialog dialog = super.onCreateDialog(savedInstanceState); 
    DialogUtils.setMargins(dialog, 0, 150, 50, 75); 
    return dialog; 
} 

इस विधि संवाद करने के लिए मार्जिन लागू करने है:

मैं onCreateDialog विधि में लागू करके एक DialogFragment के लिए मेरे दृष्टिकोण का परीक्षण किया प्रयुक्त:

@NonNull 
public static Point getDisplayDimensions(Context context) 
{ 
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); 
    Display display = wm.getDefaultDisplay(); 

    DisplayMetrics metrics = new DisplayMetrics(); 
    display.getMetrics(metrics); 
    int screenWidth = metrics.widthPixels; 
    int screenHeight = metrics.heightPixels; 

    // find out if status bar has already been subtracted from screenHeight 
    display.getRealMetrics(metrics); 
    int physicalHeight = metrics.heightPixels; 
    int statusBarHeight = getStatusBarHeight(context); 
    int navigationBarHeight = getNavigationBarHeight(context); 
    int heightDelta = physicalHeight - screenHeight; 
    if (heightDelta == 0 || heightDelta == navigationBarHeight) 
    { 
     screenHeight -= statusBarHeight; 
    } 

    return new Point(screenWidth, screenHeight); 
} 

public static int getStatusBarHeight(Context context) 
{ 
    Resources resources = context.getResources(); 
    int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android"); 
    return (resourceId > 0) ? resources.getDimensionPixelSize(resourceId) : 0; 
} 

public static int getNavigationBarHeight(Context context) 
{ 
    Resources resources = context.getResources(); 
    int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android"); 
    return (resourceId > 0) ? resources.getDimensionPixelSize(resourceId) : 0; 
} 

सहायक तरीके हैं मेरे SO answers में से एक में समझाया गया।

यह Gist में एक विस्तारित संस्करण हैं जो इमर्सव मोड का भी समर्थन करता है।

6

मैं एक ही समस्या थी और मैं एक InsetDrawable पर विंडो पृष्ठभूमि की स्थापना करके इसे हल:

AlertDialog.Builder builder = new AlertDialog.Builder(context); 
... 
... 
AlertDialog dialog = builder.create(); 

ColorDrawable back = new ColorDrawable(Color.TRANSPARENT); 
InsetDrawable inset = new InsetDrawable(back, 20); 
dialog.getWindow().setBackgroundDrawable(inset); 

dialog.show(); 

इस मामले संवाद सभी किनारों के लिए 20 की एक मार्जिन के साथ दिखाई देगा।

+0

यह नए एपीआई पर काम करता था लेकिन जब मैंने एपीआई 16 पर परीक्षण किया तो यह अलर्टडियलोग के चारों ओर एक काला सीमा दिखाता है। –

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