2012-01-15 13 views
20

के लिए अधिकतम चौड़ाई सेट करने के लिए कैसे करें बहुत सारे गुगलिंग के बाद मैं इस समस्या का हल ढूंढने में असमर्थ हूं। मेरे पास यह लेआउट है:एक लेआउट

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/adlayout" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:orientation="vertical"> 
<TableLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:stretchColumns="1" 
    android:layout_weight="1" 
    > 

    <TableRow android:id="@+id/tableRow1" android:layout_width="fill_parent" android:layout_height="wrap_content"> 
     <TextView android:padding="6dip" android:text="@string/barcode_format" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> 
     <EditText android:padding="6dip" android:layout_width="fill_parent" android:id="@+id/edit_barcode_format" android:layout_height="wrap_content"></EditText> 
    </TableRow> 
    <TableRow android:id="@+id/tableRow1" android:layout_width="fill_parent" android:layout_height="wrap_content"> 
     <TextView android:padding="6dip" android:text="@string/barcode_type" android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> 
     <EditText android:padding="6dip" android:layout_width="fill_parent" android:id="@+id/edit_barcode_type" android:layout_height="wrap_content"></EditText> 
    </TableRow>   
</TableLayout> 


</LinearLayout> 

तालिका लेआउट एक फॉर्म को परिभाषित करता है। एक फोन पर ठीक दिखता है लेकिन टैबलेट पर, संपादन दृश्य बहुत बड़े लगते हैं। मैं लेआउट के लिए अधिकतम चौड़ाई निर्धारित करना चाहता हूं, और केंद्रित केंद्र को पेंट करना चाहता हूं।

उत्तर

12

आपकी स्थिति को संभालने का उचित तरीका फोन के लिए मौजूदा फ़ाइल का उपयोग करते समय टैबलेट के लिए अपने फॉर्म व्यू को अनुकूलित करने के लिए एक अलग लेआउट फ़ाइल बनाना है। आप स्क्रीन आकार और/या उनसे जुड़े संकल्प के लिए क्वालीफायर के साथ अलग-अलग res/लेआउट निर्देशिका बनाकर ऐसा करते हैं।

आप उपलब्ध संसाधन क्वालीफायर here के बारे में अधिक पढ़ सकते हैं। यहां कई विकल्प हैं और मैं आपके आवेदन के लिए सर्वश्रेष्ठ चुनने के लिए इसे छोड़ दूंगा, लेकिन यहां एक साधारण उदाहरण है:

मान लीजिए कि आपकी वर्तमान लेआउट फ़ाइल form.xml है।

res/layout/form.xml में अपनी मौजूदा लेआउट फ़ाइल रखें। यह इसे डिफ़ॉल्ट लेआउट बना देगा।

एक और फ़ाइल बनाएं और इसे res/layout-big/form.xml पर रखें। यह लेआउट फ़ाइल भौतिक स्क्रीन आकार> ~ 5 "(सभी मानक टैबलेट) वाले उपकरणों पर उपयोग की जाएगी। अपनी समस्या को संभालने के लिए, मैंने क्षैतिज रूप से केंद्रित फॉर्म को प्रदर्शित करने के लिए अपना डिफ़ॉल्ट लेआउट संशोधित किया है और केवल स्क्रीन चौड़ाई का 60% :।

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

    <TableLayout 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.6" 
     android:stretchColumns="1" > 

     <TableRow android:id="@+id/tableRow1"> 

      <TextView 
       android:id="@+id/textView1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:padding="6dip" 
       android:text="Barcode Format" > 
      </TextView> 

      <EditText 
       android:id="@+id/edit_barcode_format" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:padding="6dip" > 
      </EditText> 
     </TableRow> 

     <TableRow android:id="@+id/tableRow1"> 

      <TextView 
       android:id="@+id/textView2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:padding="6dip" 
       android:text="Barcode Type" > 
      </TextView> 

      <EditText 
       android:id="@+id/edit_barcode_type" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:padding="6dip" > 
      </EditText> 
     </TableRow> 
    </TableLayout> 

</LinearLayout> 

परिवर्तन LinearLayout की layout_weight और weightSum गुण है, जो मेज बताने के लिए केवल अपने माता-पिता के 60% (layout_weight=0.6) भरने का इस्तेमाल करता है यह एक अधिकतम चौड़ाई सेट करने का प्रयास की तुलना में अधिक कुशल है, खासकर जब उपकरणों परिवर्तनीय संकल्प और पहलू अनुपात हैं।

एफवाईआई, मैंने आपको कई अनावश्यक विशेषताओं को हटाने की भी कोशिश की आपके एक्सएमएल में था जो कुछ भी नहीं कर रहा था लेकिन अव्यवस्था बना रहा था (जैसे एकाधिक xmlns:android घोषणाएं)। उन परिवर्तनों में से एक TableLayout बच्चों के अतिरिक्त layout_ पैरामीटर को हटा रहा था, क्योंकि TableLayout इन सभी मानकों को किसी भी तरह से अनदेखा करता है और अपने बच्चों को कुछ बाधाओं का उपयोग करने के लिए मजबूर करता है। डॉक्स से:

टेबललेआउट के बच्चे लेआउट_विड्थ विशेषता निर्दिष्ट नहीं कर सकते हैं। चौड़ाई हमेशा MATCH_PARENT है। हालांकि, लेआउट_हेइट विशेषता को बच्चे द्वारा परिभाषित किया जा सकता है; डिफ़ॉल्ट मान WRAP_CONTENT है। यदि बच्चा टेबलरो है, तो ऊंचाई हमेशा WRAP_CONTENT होती है।

आप TableLayoutin the SDK docs के बारे में अधिक पढ़ सकते हैं।

HTH

+0

ग्रेट उत्तर। बहुत बहुत धन्यवाद!!! – MaikoMobile

+0

ग्रेट सॉल्यूशन! –

1

आप एक नया ViewGroup कि LinearLayout तक फैली हुई है और चौड़ाई आप चाहते हैं के द्वारा इसे सीमित करता है बनाने के लिए के लिए लिंक या सिर्फ एक मान सेट देखें। this answer देखें।

+1

पर बाधाएं धन्यवाद लेकिन मैं एक एक्सएमएल समाधान की तलाश में था। – MaikoMobile

34

रूप @Devunwired सुझाव विभिन्न लेआउट फ़ाइलों रखते हुए सही है, तथापि, यह जटिलता अपनी परियोजना के लिए कहते हैं (आप डिजाइनर व्यू लेआउट redesigns मामले में एक से अधिक फ़ाइलों को बनाए रखने होगा)।

यदि आपको केवल एक बटन की चौड़ाई बदलने की ज़रूरत है, तो मैं निम्नलिखित का सुझाव दूंगा। लेआउट फ़ोल्डर में एक xml फ़ाइल रखें और

के एक मूल्य देना
<LinearLayout 
    style="@style/width_match_parent_max_200" 
    android:layout_height="wrap_content"> 

मूल्यों w600dp/styles.xml शामिल

<resources> 
    <style name="width_match_parent_max_200"> 
     <item name="android:layout_width">200dp</item> 
    </style> 
</resources> 

मूल्यों/styles.xml शामिल

<resources> 
    <style name="width_match_parent_max_200"> 
     <item name="android:layout_width">match_parent</item> 
    </style> 
</resources> 

संपादित करें : नोट करें कि फ़ोल्डर मान-w600dp है और मान नहीं -600dp

+0

ध्यान रखें कि 'LinearyLayout' ** ** ** ** एंड्रॉइड नहीं होगा: layout_width =" wrap_content "मैंने यह गलती की और सोचा कि इसकी आवश्यकता है, लेकिन इसकी आवश्यकता नहीं है –

+0

अच्छा समाधान, हालांकि यह मूल्य- ** डब्ल्यू ** 600dp। –

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