2013-03-04 13 views
6

मैं स्क्रीन की चौड़ाई का 50% के रूप में मेरे विचार की चौड़ाई सेट करने के लिए और उसके बाद इस दृश्य को मध्य क्षैतिज है जिसके फलस्वरूप 1 या अधिक बटन जो बाईं या के दाईं ओर से जुड़ी दिखाई कर सकते हैं होने की जरूरत है स्क्रीन।एंड्रॉयड प्रतिशत चौड़ाई लेआउट

मैं एक रिश्तेदार लेआउट का उपयोग कर रहा है, ताकि मैं वजन के साथ एक रेखीय लेआउट जगह है, जबकि कि डालूँगा आर एल के बाएं या दाएं किनारे से जुड़ी के शीर्ष पर किसी भी बटन लगाने मेरी 50% केंद्रित प्राप्त करने के लिए कर सकते हैं। हालांकि इस लेआउट में नीली मध्य बार गुम है। यदि मैं मध्य दृश्य लेआउट_वेट 1 पर सेट करता हूं तो मुझे 3 बराबर आकार के बार मिलते हैं।

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="48dp"> 

    <LinearLayout 
     android:id="@+id/stupid_android" 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 

     <View 
      android:layout_width="fill_parent" 
      android:layout_height="match_parent" 
      android:background="#FF0000" 
      android:layout_weight="1" /> 

     <View 
      android:layout_width="fill_parent" 
      android:layout_height="match_parent" 
      android:background="#0000FF" 
      android:layout_weight="2" /> 

     <View 
      android:layout_width="fill_parent" 
      android:layout_height="match_parent" 
      android:background="#00FF00" 
      android:layout_weight="1" /> 

    </LinearLayout> 

</RelativeLayout> 

उत्तर

15

आप अगर मैं सही ढंग से समझ 0dip

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="48dp"> 

    <LinearLayout 
     android:id="@+id/stupid_android" 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 

     <View 
      android:layout_width="0dip" 
      android:layout_height="match_parent" 
      android:background="#FF0000" 
      android:layout_weight="1" /> 

     <View 
      android:layout_width="0dip" 
      android:layout_height="match_parent" 
      android:background="#0000FF" 
      android:layout_weight="2" /> 

     <View 
      android:layout_width="0dip" 
      android:layout_height="match_parent" 
      android:background="#00FF00" 
      android:layout_weight="1" /> 

    </LinearLayout> 

</RelativeLayout> 
+1

के लिए यह कैसे काम करता है 'layout_weight' है। 'लेआउट_वेट' बताता है कि माता-पिता 'व्यू' में ** शेष ** स्थान बच्चों के' व्यू 'से कैसे विचलित होना चाहिए। आपके मामले में पहले 'व्यू' में 'android है: layout_width' 'fill_parent' पर सेट है, इसलिए वास्तव में कोई शेष स्थान नहीं है। मैं वास्तव में नहीं जानता कि अंतिम दृश्य अभी भी क्यों दिखाई दे रहा है, क्योंकि मैंने केवल पहले को देखने का प्रयास किया था। –

-2

को देखने की चौड़ाई निर्धारित करना चाहिए, तो आप इस की जरूरत है:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="48dp"> 

    <LinearLayout 
     android:id="@+id/stupid_android" 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <View 
      android:layout_width="fill_parent" 
      android:layout_height="match_parent" 
      android:background="#FF0000" 
      android:layout_weight="1" ></View> 

     <View 
      android:layout_width="fill_parent" 
      android:layout_height="match_parent" 
      android:background="#0000FF" 
      android:layout_weight="1.5" ></View> 

     <View 
      android:layout_width="fill_parent" 
      android:layout_height="match_parent" 
      android:background="#00FF00" 
      android:layout_weight="1.5" ></View> 

    </LinearLayout> 

</RelativeLayout> 
1

आप नए प्रतिशत पुस्तकालय के साथ इस लक्ष्य को हासिल कर सकते हैं:

https://developer.android.com/tools/support-library/features.html#percent

कुछ इस तरह कर रही द्वारा

:

<android.support.percent.PercentRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="48dp"> 

    <View 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:background="#FF0000" 
     app:layout_widthPercent="25%" /> 

    <View 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="2" 
     android:background="#0000FF" 
     android:centerHorizontal="true" 
     app:layout_marginLeftPercent="25%" 
     app:layout_widthPercent="50%" /> 

    <View 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:alignParentRight="true" 
     android:background="#00FF00" 
     app:layout_marginLeftPercent="75%" 
     app:layout_widthPercent="25%" /> 

</android.support.percent.PercentRelativeLayout> 
0

उपयोग नई प्रतिशत समर्थन पुस्तकालय।

compile 'com.android.support:percent:24.0.0' 

उदाहरण:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/main" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <android.support.percent.PercentRelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="48dp" 

     > 

     <View 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="#FF0000" 
      app:layout_widthPercent="33%" /> 

     <View 
      android:layout_height="match_parent" 
      android:layout_weight="2" 
      android:background="#0000FF" 
      app:layout_marginLeftPercent="33%" 
      app:layout_widthPercent="33%" /> 

     <View 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="#00FF00" 
      app:layout_marginLeftPercent="66%" 
      app:layout_widthPercent="33%" /> 

    </android.support.percent.PercentRelativeLayout> 


</LinearLayout> 

अधिक जानकारी Android Doc लिए

* नमूना & ट्यूटोरियल * Tutorial1Tutorial2Tutorial3

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