2011-06-08 14 views
11

मैं अपने लेआउट को अधिक लैंडस्केप अनुकूल बनाने की कोशिश कर रहा हूं, और मेरे पास एक सामान्य पैटर्न है जो कुछ बटनों (जैसे ओके | रद्द करें) के साथ LinearLayout जोड़ना है और बस layout_weight के लिए एक मान सेट करना है ताकि अंतरिक्ष समान रूप से वितरित किया जाता है। समस्या यह है कि जब आप लैंडस्केप मोड में एक फोन या (विशेष रूप से) टैबलेट का उपयोग करते हैं, तो आप हास्यास्पद दिखने वाले भारी बटन के साथ समाप्त होते हैं। मैंने बटन पर और रैखिक लेआउट पर maxWidth सेट करने का प्रयास किया लेकिन इसका कोई फायदा नहीं हुआ। इसे हासिल करने का सबसे आसान तरीका क्या होगा? अर्थात्, दृश्य के लिए अधिकतम क्षैतिज आकार सेट करना ताकि यह पूरी चौड़ाई तक नहीं बढ़े। मैंने विभिन्न चीजों का एक गुच्छा करने की कोशिश की, लेकिन कोई भी काम नहीं किया।विचारों के लिए layout_weight और maxWidth को जोड़ें

<LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center_horizontal" 
     android:paddingLeft="10dp" 
     android:paddingRight="10dp" 
     android:paddingTop="6dp" 
     android:paddingBottom="6dp"> 
     <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:enabled="false" 
     android:textStyle="bold" 
     android:text="@string/button1" /> 
     <Button 
     android:id="@+id/button2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="@string/button2" /> 
    </LinearLayout> 

नोट::

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

उत्तर

0

मैं ग्रहण यहाँ यह परीक्षण करने के लिए नहीं है, लेकिन मैं एक RelativeLayout बजाय, कुछ की तरह का प्रयोग करेंगे:

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    > 
    <View 
    android:id="@+id/centerView" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_marginLeft="5dp" 
    android:layout_marginRight="5dp" 
    /> 
    <Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_toLeftOf="@id/centerView" 
    android:enabled="false" 
    android:textStyle="bold" 
    android:text="@string/button1" 
    /> 
    <Button 
    android:id="@+id/button2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_toRightOf="@id/centerView" 
    android:text="@string/button2" 
    /> 
</RelativeLayout> 

जैसा कि मैंने कहा, मैं अभी परीक्षण नहीं कर सकते, लेकिन आप काम कर सकते हैं इस विचार के आसपास।

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

+0

BTW, यह के रूप में योग्य है [धोखाधड़ी] (http://stackoverflow.com/questions/6254303/android-programatically-select-gallary-image/6255224#6255224)? ;) – Aleadam

+0

हे, नहीं, यह अच्छा है। मैं इसे आज़मा रहा हूं और बहुत अच्छी तरह से काम करता हूं, इस तथ्य को छोड़कर कि रिलेवेटलायआउट ने मेरे ऊपर एडिटटेक्स्ट खा लिया है। लेकिन, यह आशाजनक लग रहा है! – dmon

+0

@dmon आपको प्रत्येक दृश्य में 'android: layout_below =" @ id/textView1 "' (या जो भी आईडी है) जोड़ना होगा। सापेक्ष लयआउट एक बहुत ही शक्तिशाली लेआउट है। सभी संभावित [यहां] (http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html) पर एक नज़र डालें। – Aleadam

1

सापेक्ष लेआउट के साथ काफी हद तक गड़बड़ करने के बाद, मैंने this answer पर ठोकर खाई, जिसका उपयोग मैंने समाप्त किया।

+0

मुझे लगता है कि आप उस पृष्ठ पर "लाइनरलाइट का विस्तार करें और ऑनमीर फ़ंक्शन ओवरराइड करें" का अर्थ लेंगे। पृष्ठ का एक और हालिया उत्तर है, "एक बाहरी लेआउट जोड़ें" http://stackoverflow.com/questions/5875877/setting-a-maximum-width-on-a-viewgroup#23072507 जो बहुत आसान है (लेकिन मैंने नहीं किया ' टी परीक्षण)। – Jerry101

-1
<RelativeLayout 
    android:id="@+id/rlMain" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_marginLeft="12dp" 
    android:layout_marginRight="12dp"> 

    <View 
     android:id="@+id/vCenter" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:layout_centerHorizontal="true" 
     android:layout_marginRight="5dp" 
     android:layout_marginLeft="5dp"/> 

    <Button 
    android:id="@+id/btnOne" 
    android:layout_width="wrap_content" 
    android:layout_height="40dp" 
    android:layout_toLeftOf="@id/vCenter" 
    <!--Change this for different widths--> 
    android:minWidth="200dp"/> 

    <Button 
    android:id="@+id/btnTwo" 
    android:layout_width="wrap_content" 
    android:layout_height="40dp" 
    android:layout_toRightOf="@id/vCenter" 
    <!--Change this for different widths--> 
    android:minWidth="200dp"/> 
</RelativeLayout> 
+0

भविष्य के पाठकों के लिए आपके उत्तर में थोड़ा स्पष्टीकरण बेहतर है? – HaveNoDisplayName

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

  • कोई संबंधित समस्या नहीं^_^