2016-02-09 8 views
5

मेरे पास कुछ एक्सएमएल है। इस एक्सएमएल में रिश्तेदार लेआउट के अंदर एक बटन है। सापेक्ष लेआउट पूरी स्क्रीन लेता है। मैं चाहता हूं कि बटन स्क्रीन चौड़ाई और ऊंचाई के 1/3 हो। मैं ये कैसे करूं?Android रिश्तेदार लेआउट में स्क्रीन चौड़ाई और ऊंचाई के बटन 1/3 बनाते हैं

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingLeft="0dp" 
    android:paddingRight="0dp" 
    android:paddingTop="0dp" 
    android:paddingBottom="0dp" 
    tools:context="com.vroy.trapper.MainMenuActivity" 
    android:layout_weight="2"> 

    <Button 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:text="@string/menu_button_text" 
     android:background="@drawable/round_button" 
     android:id="@+id/button" 
     android:layout_centerVertical="true" 
     android:layout_centerHorizontal="true" 
     /> 
</RelativeLayout> 

मैं this सवाल को देखा है, लेकिन फिर भी मैं रिश्तेदार लेआउट ही करने के लिए एक वेट किया मैं बटन के लिए एक वजन आवंटित नहीं कर सकता:

यहाँ एक्सएमएल है।

उत्तर

8

आप इसे आसानी से जावा कोड में कर सकते हैं।

इस कोड को ऑनक्रेट में लिखें।

Button b=(Button) findViewById(R.id.button); 
    int width = getResources().getDisplayMetrics().widthPixels/3; 
    int hei=getResources().getDisplayMetrics().heightPixels/3; 
    b.setLayoutParams(new RelativeLayout.LayoutParams(width,hei)); 
0

अपने लक्ष्य को प्राप्त करने के लिए रिलेवेटिवआउट के बजाय लीनियरलाउट का उपयोग करें।

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal"> 

    <View 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     /> 
    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:orientation="vertical"> 
     <View 
      android:layout_width="wrap_content" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      /> 
     <Button 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:text="@string/menu_button_text" 
      android:background="@drawable/round_button" 
      android:id="@+id/button"/> 
      <View 
      android:layout_width="wrap_content" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      /> 
    </LinearLayout> 
    <View 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     /> 

    </LinearLayout> 

उम्मीद है कि यह मदद करता है !!

+0

समस्या यह है कि मैं अपने समग्र लेआउट के लिए एक सापेक्ष लेआउट चाहता हूं। – Roymunson

+0

मैं समझता हूं लेकिन रिलेवेटिवआउट में वजन का उपयोग करना संभव नहीं है, यह तत्व लीनियरलाउट के अंदर विजेट के लिए है :(... लेकिन आप रिलेवेटलाइट का उपयोग अपने मूल लेआउट के रूप में कर सकते हैं और इसके अंदर मेरे पोस्टरलायआउट को एंड्रॉइड के साथ रख सकते हैं: layout_width = "match_parent" और एंड्रॉइड: layout_height = "match_parent"। यह एक चाल का उपयोग है रिलेवेटलाइटआउट लाइनरलायआउट –

2

(चौड़ाई पर विचार करने के लिए भी संपादित) एक रैखिक लेआउट के चारों ओर बटन लपेटें।

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="0dp" 
    android:paddingLeft="0dp" 
    android:paddingRight="0dp" 
    android:paddingTop="0dp"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="horizontal" 
     android:gravity="center" 
     android:weightSum="3" > 
     <LinearLayout 
      android:layout_width="0dp" 
      android:layout_weight="1" 
      android:layout_height="match_parent" 
      android:orientation="vertical" 
      android:gravity="center" 
      android:weightSum="3"> 

      <Button 
       android:id="@+id/button" 
       android:layout_width="match_parent" 
       android:layout_height="0dp" 
       android:layout_centerHorizontal="true" 
       android:layout_centerVertical="true" 
       android:layout_weight="1" 
       android:text="@string/menu_button_text" 
       android:background="@drawable/round_button" 
       /> 
     </LinearLayout> 
    </LinearLayout> 
</RelativeLayout> 

बदलें android:gravity अगर आप अपने बटन दाएँ/बाएँ आदि इस के साथ रहना चाहता हूँ, आप अभी भी अपने रिश्तेदार लेआउट इसके चारों ओर अन्य बातों के लिए इस्तेमाल कर सकते हैं।

+0

के साथ संयुक्त कोड का आपका संस्करण बटन को स्क्रीन की ऊंचाई के 1/3 लेता है। लेकिन स्क्रीन की चौड़ाई के 1/3 नहीं शायद ऐसा इसलिए है क्योंकि आप चौड़ाई को "match_parent" '? – Roymunson

+0

पर सेट करते हैं, मैंने अतिरिक्त रैखिक लयआउट जोड़कर चौड़ाई पर भी विचार करने के लिए संपादित किया है। चीजों को करने का सबसे अच्छा तरीका नहीं है, क्योंकि यह नेस्टेड लेआउट है। – Elye

+0

सर्वश्रेष्ठ उत्तर, धन्यवाद बहुत कुछ! – middlehut

4

प्रतिशत समर्थन लाइब्रेरी आपको जिस चीज की आवश्यकता है वह है।

यह लाइब्रेरी उपयोग करने में बहुत आसान है क्योंकि यह केवल एक ही सापेक्ष लयआउट और फ़्रेमलाउट है जो हम कुछ अतिरिक्त कार्यक्षमताओं के साथ परिचित हैं।

सबसे पहले, चूंकि प्रतिशत समर्थन लाइब्रेरी एंड्रॉइड सपोर्ट लाइब्रेरी 23 के साथ आता है, इसलिए कृपया सुनिश्चित करें कि आप पहले से ही नवीनतम संस्करण में एसडीके प्रबंधक में एंड्रॉइड सपोर्ट लाइब्रेरी अपडेट करते हैं।

संकलन 'com.android.support:percent:23.0.0'

अब वापस अपने प्रश्न के लिए आते हैं, तो आप कुछ कर सकते हैं: और फिर फ़ाइल build.gradle में नीचे की तरह एक निर्भरता को जोड़ने अपने आउटपुट को प्राप्त करने के लिए इस तरह। आशा है इससे आपकी मदद होगी।

<?xml version="1.0" encoding="utf-8"?> 
<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="match_parent"> 

<Button 
    android:id="@+id/button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" 
    android:background="@drawable/bird" 
    android:text="Your text" 
    app:layout_heightPercent="33%" 
    app:layout_widthPercent="33%" /> 
</android.support.percent.PercentRelativeLayout> 
+1

अगर मेरे उत्तर ने आपकी मदद की तो कृपया वोट दें। – Rahul

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