2011-09-09 8 views
11

मैं एक कस्टम संवाद बनाने की कोशिश कर रहा हूं, और इसकी सामग्री केंद्रित है, लेकिन यह हमेशा बाएं-गठबंधन को समाप्त कर रहा है। यहाँ aboutdialog.xml है:एंड्रॉइड संवाद के अंदर लेआउट कैसे केंद्रित करें?

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:a="http://schemas.android.com/apk/res/android" 
    a:orientation="vertical" 
    a:id="@+id/AboutDialogLayout" 
    a:layout_width="fill_parent" 
    a:layout_height="fill_parent" 
    a:layout_gravity="center_horizontal" 
    a:gravity="center_horizontal"> 

    <ImageView a:id="@+id/imageView1" 
       a:src="@drawable/pricebook" 
       a:layout_height="wrap_content" 
       a:layout_width="wrap_content"/> 
    <TextView a:layout_height="wrap_content"   
       a:textAppearance="?android:attr/textAppearanceLarge" 
       a:id="@+id/textView1" 
       a:text="Price Book" 
       a:layout_width="wrap_content"/> 
    <TextView a:layout_height="wrap_content" 
       a:id="@+id/textView2" 
       a:layout_width="wrap_content" 
       a:text="foo"/> 
</LinearLayout> 

और मेरे (प्रासंगिक) कोड:

Dialog d = new Dialog(this); 
d.setContentView(R.layout.aboutdialog); 
d.setTitle(R.string.app_name); 

क्या मैं यहाँ याद आ रही है? सहायता के लिए धन्यवाद।

छवि: Screen shot of Dialog problem

+0

यह अजीब है। यह मेरे लिए ठीक काम करता है। क्या आप स्क्रीनशॉट पोस्ट कर सकते हैं? – inazaruk

+3

+1 का उपयोग करने के लिए +1: एंड्रॉइड के बजाए: हर जगह, आपने यह कैसे किया? –

+0

हां, मैं भी इसे अपने कामकाजी जुर्माना की जांच करता हूं। –

उत्तर

9

कोशिश करना होगा: सापेक्ष wrap_content के लिए fill_parent साथ a:layout_gravity="center_horizontal|center_vertical"
या
उपयोग Relative_Layout और माता पिता के केंद्र के लिए रेखीय लेआउट संरेखित रैखिक के लिए।

हालांकि यह केन्द्र पर मेरे साथ काम किया है

+3

एक रिलेवेटिवआउट में पूरे एक्सएमएल को लपेटकर यह किया। * लेआउट्स दोनों के लिए 'लेआउट_विड्थ' 'fill_parent' होना चाहिए और 'layout_height' दोनों को' wrap_content' पर सेट करने की आवश्यकता है (कम से कम मैं इसे कैसे चाहता हूं)। – Conrad

+0

सापेक्ष लेआउट चाल था, धन्यवाद। –

+0

लेकिन समस्या यह है कि RelativeLayout में बच्चे ViewGroup में यह कार्य करने के लिए layout_width = fill_parent होना चाहिए और कभी-कभी आप अपनी सामग्री के लिए उस व्यवहार को नहीं चाहते हैं। नीचे मेरा समाधान देखें;) –

0

आप layout_gravity जो अच्छा है, लेकिन जब से तुम यह outmost लेआउट के लिए करते हैं, यह अपने आप में क्या यह चारों ओर है के सापेक्ष स्थिति नहीं कर सकते हैं कर रहे हैं।

मुझे लगता है कि एक और लेआउट के अंदर ऊपर लेआउट लपेटकर यह यह करने के लिए संपादित करने के लिए

+0

उपरोक्त एक्सएमएल को अन्य रैखिक लयआउट में लपेटने से कोई फर्क नहीं पड़ता है। – Conrad

0

तुम सिर्फ गुरुत्वाकर्षण के अपने LinearLayout layout_gravity बदलने का प्रयास कर सकते हैं? यहां वर्णित अंतर देखें: Gravity and layout_gravity on Android

+0

कोई फर्क नहीं पड़ता। – Conrad

+0

आपके LinearLayout के अंदर प्रत्येक दृश्य पर layout_gravity = "center" को सेट करने के बारे में कैसे? –

+0

ने पहले से ही कोशिश की - अभी भी कोई पासा नहीं है। – Conrad

2

डायलॉग के व्यू ग्रुप (फ्रेमलेआउट) को संशोधित करें जिसमें आपकी सामग्री लेआउट है। मैं स्थैतिक विधि का उपयोग करता हूं और डायलॉगफ्रेगमेंट्स में एक्टिविटी क्रिएटेड() के अंदर इसे आमंत्रित करता हूं:

/** 
* Center dialog content inside available space 
* @param dialog 
*/ 
public static void centerDialogContent(Dialog dialog) { 
ViewGroup decorView = (ViewGroup) dialog.getWindow().getDecorView(); 
View content = decorView.getChildAt(0); 
FrameLayout.LayoutParams contentParams = (FrameLayout.LayoutParams)content.getLayoutParams(); 
contentParams.gravity = Gravity.CENTER; 
content.setLayoutParams(contentParams); 
} 
संबंधित मुद्दे