2010-04-26 17 views
26

मेरे पास LinearLayout है जिसमें पृष्ठभूमि छवि (9 पेटेड पीएनजी फ़ाइल) है। मैं बाएं और दाएं पैडिंग कैसे जोड़ सकता हूं ताकि पृष्ठभूमि छवि पूरी चौड़ाई नहीं लेती? मैंने android:paddingLeft और android:paddingRight को आजमाया है, लेकिन यह कुछ भी नहीं बदलेगा।पृष्ठभूमि छवि के लिए पैडिंग कैसे जोड़ें

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:paddingLeft="25dip" 
    android:paddingRight="25dip" 
    android:background="@drawable/background"> 

पूरी पृष्ठभूमि अभी भी पूरी स्क्रीन चौड़ाई फैली हुई है।

उत्तर

3

यह काम नहीं करता है क्योंकि पैडिंग केवल LinearLayout की सामग्री पर कार्य करती है। इस के अंदर एक दूसरे LinearLayout का उपयोग करके पैडिंग प्रभावी हो जाएगी। आपको पहले LinearLayout के पृष्ठभूमि रंग को परिभाषित करना होगा जो पैडिंग क्षेत्र में दिखाई देगा।

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:paddingLeft="25dip" 
    android:paddingRight="25dip" 
    android:background="#FF000000"> 

    <LinearLayout 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/background"> 

    </LinearLayout> 

</LinearLayout> 

नोट: यह संभवतः पृष्ठभूमि के लिए एक XML फ़ाइल का उपयोग करके भी संभव है।

+0

यह इतना अच्छा नहीं है के रूप में यह पदानुक्रम गहराई बढ़ जाती है। क्या आप XML के साथ इसे कार्यान्वित करने के विवरण के साथ उत्तर का विस्तार कर सकते हैं? –

+0

मैंने इन्सेट ड्रायबल का उपयोग करके एक और समाधान पोस्ट किया है, इसलिए विस्तार करने की कोई आवश्यकता नहीं है। धन्यवाद। –

+1

आप फ्रेमरआउट – vovahost

83

आप एक्सएमएल drawable, विशेष रूप से इस उद्देश्य के लिए तैयार किया गया है का उपयोग करना चाहिए - इनसेट Drawable: http://developer.android.com/guide/topics/resources/drawable-resource.html#Inset

उदाहरण के लिए:

रेस/drawable/inset_background.xml:

<?xml version="1.0" encoding="utf-8"?> 
<inset xmlns:android="http://schemas.android.com/apk/res/android" 
    android:drawable="@drawable/your_background_image" 
    android:insetRight="25dip" 
    android:insetLeft="25dip" /> 

और फिर आपके एक्सएमएल लेआउट में:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/inset_background"> 
+0

के साथ बाहरी लीनियरआउट को बदलकर इस लेआउट को बेहतर बना सकते हैं। मेरे पास मेरे लिए अपेक्षित परिणाम नहीं है, मेरे पास यह वही सटीक समस्या है http://stackoverflow.com/questions/26850400/inset-drawable-not- काम-जैसा-वर्णित-इन-प्रलेखन –

2

तुम बस

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="horizontal" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:layout_marginLeft="25dip" 
android:layout_marginRight="25dip" 
android:background="@drawable/background"> 
5

उर पृष्ठभूमि drawable उपयोग कर सकते हैं इस तरह होना चाहिए, एंड्रॉयड का उपयोग कर: नीचे

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:bottom="48dp" android:drawable="@drawable/bg"> 
    </item> 

</layer-list> 
+0

यह समाधान वास्तव में काम करता है !! –

+0

आपने मेरी जिंदगी बचाई है। – viper

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