2011-07-22 18 views
35

हाय सब मैंने अभी एक लिनियरलाउट के अंदर एक सूची दृश्य लागू किया है, लेकिन मुझे लाइनरलाइट की ऊंचाई को परिभाषित करने की आवश्यकता है (यह स्क्रीन ऊंचाई का 50% होना चाहिए)।एंड्रॉइड: लेआउट_हेइट स्क्रीन आकार का 50%

<LinearLayout 
    android:id="@+id/widget34" 
    android:layout_width="300px" 
    android:layout_height="235px" 
    android:orientation="vertical" 
    android:layout_below="@+id/tv_scanning_for" 
    android:layout_centerHorizontal="true"> 

    <ListView 
     android:id="@+id/lv_events" 
     android:textSize="18sp"   
     android:cacheColorHint="#00000000" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/tv_scanning_for" 
     android:layout_centerHorizontal="true"> 
    </ListView> 

</LinearLayout> 

क्या यह संभव है?

मैंने बटन और एडिटटेक्स्ट के लिए कुछ ऐसा किया, लेकिन लेआउट पर काम नहीं कर रहा है।

//capture the size of the devices screen 
    Display display = getWindowManager().getDefaultDisplay(); 
    double width = display.getWidth(); 

    //my EditText will be smaller than full screen (80%)   
    double doubleSize = (width/5)*4; 
    int editTextSize = (int) doubleSize; 

    //define the EditText 
    userName = (EditText) this.findViewById(R.id.userName); 
    password = (EditText) this.findViewById(R.id.password); 

    //set the size 
    userName.setWidth(editTextSize); 
    password.setWidth(editTextSize); 

धन्यवाद:

यह मेरा कोड है! :)

+0

क्या आपने लीनियरलाउट के आसपास एक रिलेवेटिवआउट का उपयोग करने का प्रयास किया है और रिलेटिव लयआउट का आकार लिया है? –

+0

क्या मैं रिलेवेटिवआउट ऊंचाई पैरामीटर में प्रतिशत दर्ज कर सकता हूं? –

+0

कस्टम लेआउट पैराम का उपयोग करें: http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html RelativeLayout.setLayoutParams(): http://developer.android.com/reference/android/view/View के साथ .html # setLayoutParams% 28android.view.ViewGroup.LayoutParams% 29। प्रदर्शित करने के लिए ऊंचाई सेट करें .getHeight()/2 –

उत्तर

70

अपने layout_height="0dp" *, एक layout_height भी 0dp के बराबर के साथ एक खाली View उसके नीचे (या खाली ImageView या सिर्फ एक FrameLayout) जोड़ने के लिए, और दोनों दृश्य सेट करने के लिए एक layout_weight="1"

यह प्रत्येक विस्तृत होंगी सेट स्क्रीन को भरने के समान ही देखें। चूंकि दोनों के पास वही वज़न है, प्रत्येक में स्क्रीन का 50% हिस्सा लगेगा।

* एडैम्प की टिप्पणी देखें कि यह क्यों काम करता है और अन्य वास्तव में उपयोगी टिप्स।

+0

यह ठीक है कि मैं इसे कैसे करूँगा। – Rob

+17

जब आप वजन का उपयोग कर रहे हों, तो याद रखें कि वजन घटाने के बाद * बचे हुए * स्थान को सभी बच्चे के विचारों के बाद मापा जाता है। यदि आप केवल वज़न पर भरोसा करना चाहते हैं तो आपको 'wrap_content' के बजाय' layout_height = "0dp" सेट करना होगा। इसके अलावा आप कुल वजन को पैड करने के लिए खाली दृश्य जोड़ने के बजाय स्पष्ट रूप से लाइनरलाउट पर 'वेटसम' सेट कर सकते हैं। – adamp

+0

@adamp यह वास्तव में उत्कृष्ट जानकारी है जिसे मैं नहीं जानता था। बहुत धन्यवाद। – LeffelMania

6

यह xml में करना आसान है। अपने शीर्ष कंटेनर को एक LinearLayout होने के लिए सेट करें और अपनी इच्छानुसार अभिविन्यास विशेषता सेट करें। फिर उस जगह के अंदर दो लाइनरलेआउट्स जिनमें दोनों चौड़ाई और ऊंचाई पर "पैरेंट भरें" हैं। अंत में, 1.

0

को उन दो linearlayouts की weigth विशेषता निर्धारित आप ऐसा ही कुछ करना चाहिए:

<LinearLayout 
    android:id="@+id/widget34" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:layout_below="@+id/tv_scanning_for" 
    android:layout_centerHorizontal="true"> 

    <ListView 
     android:id="@+id/lv_events" 
     android:textSize="18sp"   
     android:cacheColorHint="#00000000" 
     android:layout_height="1" 
     android:layout_width="fill_parent" 
     android:layout_weight="0dp" 
     android:layout_below="@+id/tv_scanning_for" 
     android:layout_centerHorizontal="true" 
     /> 

</LinearLayout> 

इसके अलावा डीपी बजाय पिक्सल का उपयोग करें या यह here बारे में पढ़ा। गतिविधि layout_height = 50%:

+2

अपनी ऊंचाई और वजन मूल्यों को स्वैप करें और यह सही होगा। ;) – adamp

+0

हां, यह सही होगा, लेकिन केवल वर्तमान डिवाइस रिज़ॉल्यूशन के लिए। क्या होगा यदि परीक्षण विभिन्न संकल्प पर है? फिर नए डिस्प्ले रिज़ॉल्यूशन के आधार पर वह 50% कम हो जाएगा! और यह ऊंचाई/चौड़ाई के लिए स्थिर मूल्यों के साथ थोड़ी मुश्किल है! यूआई घटकों के लिए आनुपातिक आयामों का उपयोग कैसे करें सीखें! – nenito

+0

वजन और ऊंचाई अभी भी मूल्यों का आदान-प्रदान कर चुकी है। –

5

यह मेरा android है

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:id="@+id/alipay_login" 
     style="@style/loginType" 
     android:background="#27b" > 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/taobao_login" 
     style="@style/loginType" 
     android:background="#ed6d00" > 
    </LinearLayout> 

</LinearLayout> 

शैली:

<style name="loginType"> 
    <item name="android:layout_width">match_parent</item> 
    <item name="android:layout_height">match_parent</item> 
    <item name="android:layout_weight">0.5</item> 
    <item name="android:orientation">vertical</item> 
</style> 
0

यह इतना आसान है कि अगर आप अपने स्क्रीन दो भाग खड़ी विभाजित करना चाहते हैं (top30% + bottom70%)

<LinearLayout 
      android:id="@+id/LinearLayoutTop" 
      android:orientation="vertical" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="2"> 

    </LinearLayout> 
    <LinearLayout 
      android:id="@+id/LinearLayoutBottom" 
      android:orientation="vertical" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="1"> 

    </LinearLayout> 
3

सबसे अच्छा तरीका है

का उपयोग करें

layout_height = "0dp" layout_weight = "0.5"

उदाहरण के लिए

<WebView 
    android:id="@+id/wvHelp" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="0.5" /> 

<TextView 
    android:id="@+id/txtTEMP" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="0.5" 
    android:text="TextView" /> 

WebView, TextView स्क्रीन ऊंचाई का 50%

0

सुनिश्चित करने के लिए है एक दृश्य की ऊंचाई है स्क्रीन का 50% तो हम एक लाइनरलाउट में दो उप लाइनरलाइट्स बना सकते हैं। प्रत्येक बच्चे लीनियरलाउट में "एंड्रॉइड होना चाहिए: 0" लेआउट_वेट "।5 आधी स्क्रीन

कवर करने के लिए माता-पिता LinearLayout होना चाहिए "एंड्रॉयड: अभिविन्यास" खड़ी

करने के लिए सेट।

यहाँ

आपके संदर्भ के लिए कोड है .... इस कोड को ऊंचाई के दो बटन आधी स्क्रीन शामिल

<LinearLayout 
android:orientation="vertical" 
android:layout_height="match_parent"> 

<LinearLayout 

    android:layout_weight="0.5" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 

    <Button 
     android:padding="10dp" 
     android:layout_weight="0.5" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:text="button1" 
     android:id="@+id/button1" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_alignParentBottom="true" 
     /> 

    <Button 
     android:padding="10dp" 
     android:layout_weight="0.5" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:text="button2" 
     android:id="@+id/button2" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" 
     android:layout_alignParentBottom="true" 
     /> 

    </LinearLayout> 

<LinearLayout 

    android:layout_weight="0.5" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 


    </LinearLayout> 
    </LinearLayout> 
0

मेरे लिए काम किया इस तरह की। हालांकि एफएबी स्वतंत्र रूप से तैरता नहीं है, लेकिन अब इसे धक्का नहीं दिया जा रहा है।

LinearLayout अंदर दिया

<LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      android:id="@+id/andsanddkasd"> 

      <android.support.v7.widget.RecyclerView 
       android:id="@+id/sharedResourcesRecyclerView" 
       android:layout_width="match_parent" 
       android:layout_height="0dp" 
       android:layout_weight="4" 
       /> 

      <android.support.design.widget.FloatingActionButton 
       android:id="@+id/fab" 
       android:layout_width="wrap_content" 
       android:layout_height="0dp" 
       android:layout_gravity="bottom|right" 
       android:src="@android:drawable/ic_input_add" 
       android:layout_weight="1"/> 

     </LinearLayout> 

आशा इस मदद करता है :)

0

यह उपलब्धि हासिल करने के लिए, एक weightSum={amount of weight to distribute} के साथ एक बाहरी रेखीय लेआउट को परिभाषित वजन का निरीक्षण करें।

यह अधिकतम वजन राशि को परिभाषित करता है। यदि निर्दिष्ट नहीं है, तो सभी बच्चों के लेआउट_वेट को जोड़कर योग की गणना की जाती है। इसका उपयोग उदाहरण के लिए कुल उपलब्ध स्थान का 50% एक कुल देने के लिए 0.5 का लेआउट_वेट देकर और वजन घटाने को 1.0 तक सेट करने के लिए किया जा सकता है। अन्य उदाहरण वजन सेट किया जाएगासमूह = 2, और यदि दोनों बच्चे layout_weight=1 सेट करते हैं तो प्रत्येक उपलब्ध स्थान का 50% मिलेगा।

वेटसूम मूल लेआउट में बच्चों की मात्रा पर निर्भर है।

+0

इस मामले में बच्चे कोई भी दृश्य समूह (फ़्रेमलाउट) या छवि दृश्य आदि जैसे किसी भी दृश्य हो सकते हैं। – Remario

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