2010-07-20 18 views
24

निम्न XML कोड एक LinearLayout में एक imageView केंद्र के लिए काम नहीं करता है:एक LinearLayout में एक ImageView कैसे केंद्रित करें?

<?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"> 
    <ImageView 
     android:id="@+id/myimg" 
     android:layout_width="36dip" 
     android:layout_height="36dip" 
     android:scaleType="fitXY" 
     android:layout_gravity="center_horizontal" 
     android:background="@drawable/my_background" 
    /> 
</LinearLayout> 

और यह RelativeLayout कोड है जो आश्चर्यजनक रूप से काम करता है:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 
    <ImageView 
     android:id="@+id/myimg" 
     android:layout_width="36dip" 
     android:layout_height="36dip" 
     android:scaleType="fitXY" 
     android:layout_centerHorizontal="true" 
     android:background="@drawable/my_background" 
    /> 
</RelativeLayout> 

कोई मुझे बता सकते हैं क्यों? धन्यवाद! layout_width = "fill_parent" LinearLayout में:

+0

मैं गुरुत्वाकर्षण सही ढंग से काम विशेषताओं कभी नहीं मिल सकता है प्रदान करने के लिए करना चाहता था। मैं सिर्फ सापेक्ष लेआउट के साथ जाऊंगा। – Falmarri

+0

स्केलिंग छवि (scaleType) के बिना आप कर सकते हैं कि एंड्रॉयड बदलकर: "fill_parent" करने के लिए layout_width = "36dip"। –

+2

मुझे हमेशा गुरुत्वाकर्षण के साथ परेशानी होती है। एक चीज जो आप कोशिश कर सकते हैं वह है * parent * - layerarLayout पर layout_gravity विशेषता को रखना - ImageView के बजाय। – MatrixFrog

उत्तर

2

समस्या है क्योंकि आप एंड्रॉयड का उपयोग करें। यह "wrap_content"

+0

दुर्भाग्यवश, यह केवल रैपिंग करता है: कुछ अधिक रैखिक स्तर के अंदर ImageView को केंद्रित करने के बजाय ImageView के लिए रैखिक लेआउट सीमाएं व्यावहारिक रूप से बराबर होती हैं (संभावित पैडिंग और मार्जिन के संबंध में)। –

28

आप अपने LinearLayout पर लम्बवत अभिविन्यास सेट करने के लिए

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:orientation="vertical"> 
<ImageView 
    android:id="@+id/myimg" 
    android:layout_width="36dip" 
    android:layout_height="36dip" 
    android:scaleType="fitXY" 
    android:layout_gravity="center_horizontal" 
    android:background="@drawable/my_background" 
/> 

+0

यह मेरे लिए काम किया, अच्छा जवाब –

7

क्या आम तौर पर के लिए मैं काम करता है, मैं एक RelativeLayout अंदर imageView लपेट जब फिर एक में चला जाता है है को बदलें LinearLayout यह सुनिश्चित करने के लिए कि relativelayout की ऊंचाई और चौड़ाई विशेषताओं रैखिक Layout से मेल खाता है। और आप जानते हैं कि रिलेटिवेलआउट के केंद्र में इमेजव्यू को कैसे रखा जाए। केंद्र के लिए अपने LineraLayout की गंभीरता संपत्ति:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content"> 
       <RelativeLayout 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" 
         <ImageView 
          android:id="@+id/myimg" 
          android:layout_width="36dip" 
          android:layout_height="36dip" 
          android:scaleType="fitXY" 
          android:orientation="vertical" 
          android:layout_gravity="center_horizontal" 
          android:background="@drawable/my_background" 
         /> 
       </RelativeLayout> 
    </LinearLayout> 
+2

LinearLayout लोगों के साथ क्या गलत है ??? –

2
<LinearLayout 
    android:orientation="horizontal" 
    android:layout_width="0dp" 
    android:layout_weight="0.50" 
    android:gravity="center" 
    android:layout_height="wrap_content"> 
    <ImageView 
     android:id="@+id/addToDateOrReceipt" 
     android:layout_width="25dp" 
     android:layout_height="25dp" /> 
</LinearLayout> 

एंड्रॉयड सेट करें।

12

क्षमा करें, लेकिन कोड मेरे लिए काम नहीं किया ऊपर है, इसलिए मैं अपने कोड जो मेरे लिए काम किया पोस्टिंग

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/colorPrimary" 
    android:gravity="center_horizontal|center_vertical" 
    android:orientation="vertical"> 

    <ImageView 
     android:id="@+id/ivPic" 
     android:layout_width="70dp" 
     android:layout_height="70dp" /> 

</LinearLayout> 

मुझे आशा है कि यह जो दूसरों की जरूरत के लिए मदद करता है।

+1

यह मेरे लिए उपयोगी है, तो, मैं वोट दें अपने ans :) –

+1

पूरी तरह से me.Thanks के लिए काम किया !! –

0

तुम भी एक FrameLayout साथ इसे बनाया जा सकता है और इसके अंदर एक LinearLayout तो यह दृश्य के केन्द्र पर डाल करने के लिए प्रबंधन करने के लिए आसान और आसान है।

<?xml version="1.0" encoding="utf-8"?> 

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 
<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:orientation="vertical"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/app_name" /> 

    <ImageView 
     android:id="@+id/ivPic" 
     android:layout_width="70dp" 
     android:layout_height="70dp" /> 
</LinearLayout> 
</FrameLayout> 

मैं जानता हूँ कि आप कैसे एक लेआउट में एक imageView केंद्र के लिए बारे में पूछ रहे हैं, लेकिन एक और तरीका है करने के लिए यह

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