2013-05-09 6 views
10

मैं 6 बटन के साथ एक लेआउट बनाने की कोशिश कर रहा हूं जो स्वचालित रूप से स्क्रीन के आकार को विंडोज फोन की टाइल के रूप में अनुकूलित करता है। कोड में मैं लाइन के लिए गतिशील रूप से 6 बटन, 2 बना देता हूं लेकिन बटन को बाद वाले भरने वाली स्क्रीन के आकार में फिट होना चाहिए। मैं कैसे आगे बढ़ सकता हूं?विंडोज टाइल्स जैसे 6 बटन के साथ लेआउट कैसे बनाएं

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

<LinearLayout 
    android:layout_width="match_parent" 
     android:layout_height="0dip" 
    android:orientation="horizontal" 
    android:weightSum="2" > 

    <Button 
     android:layout_width="0dip" 
       android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="@drawable/conv_up" /> 

    <Button 
     android:layout_width="0dip" 
       android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="@drawable/conv_up" 
     /> 

</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
     android:layout_height="0dip" 
    android:orientation="horizontal" 
    android:weightSum="2" > 

    <Button 
     android:layout_width="0dip" 
       android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="@drawable/conv_up" 
     /> 

    <Button 
     android:layout_width="0dip" 
       android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="@drawable/conv_up" 
     /> 

</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
     android:layout_height="0dip" 
    android:orientation="horizontal" 
    android:weightSum="2" > 

    <Button 
     android:layout_width="0dip" 
       android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="@drawable/conv_up" 
     /> 

    <Button 
     android:layout_width="0dip" 
       android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="@drawable/conv_up" 
     /> 

</LinearLayout> 

+0

रखो अब तुम क्या देखते हैं, और आप क्या उम्मीद करते हैं। अन्यथा जवाब देना मुश्किल होगा। – Siddharth

+0

मैं कुछ भी नहीं देख सकता क्योंकि यह कोड मुझे यह त्रुटि देता है: "संदिग्ध आकार: यह दृश्य अदृश्य कर देगा, लेआउट_वेट के साथ प्रयोग किया जाना चाहिए" LinearLayout एंड्रॉइड पर: layout_height = "0dip" – bisemanu

+0

'0dip' मुद्दा है। – Siddharth

उत्तर

16

पर एक नज़र रखना चाहिए मैं का उपयोग करेंगे एक ऊर्ध्वाधर LinearLayout बच्चों के रूप में एक ही वजन के तीन पंक्तियों, प्रत्येक पंक्ति एक क्षैतिज LinearLayout होने दो के बच्चों के साथ किया जा रहा है वही वजन, जो सुनिश्चित करेगा कि पूरा क्षेत्र भर गया है। छह बटन प्रदर्शन के लिए एक मुद्दा नहीं होना चाहिए।

यदि प्रदर्शन एक चिंता है, तो आप पंक्तियों को RelativeLayout एस के रूप में बना सकते हैं और आधे भाग में विभाजित करने के लिए एक स्ट्रैट का उपयोग कर सकते हैं और उस पर आधारित दो बच्चों को स्थिति दे सकते हैं।

<View android:id="@+id/strut" 
    android:layout_width="0dp" 
    android:layout_height="0dp" 
    android:layout_centerHorizontal="true"/> 

अपडेट:: आप LinearLayout रों कोशिश कर रहे हैं के बाद से, यहाँ आप ऊंचाई और चौड़ाई के साथ कैसे पेश कर सकते हैं:

जब मैं कहता हूँ एक अकड़, मैं इसका मतलब यह

माता पिता LinearLayout हो सकता है:

android:layout_width="match_parent" 
android:layout_height="match_parent" 

तीन LinearLayout बच्चों होगा:

android:layout_width="match_parent" 
android:layout_height="0dip" 

Button रों होगा:

android:layout_width="0dip" 
android:layout_height="match_parent" 

तुम नोटिस कर सकते हैं, हम संपत्ति है कि वजन पर (या तो ऊंचाई पर अगर लागू किया जाता है के लिए 0dip है माता-पिता ऊर्ध्वाधर उन्मुख है, या चौड़ाई अगर माता-पिता क्षैतिज उन्मुख है), जो अंतरिक्ष को भरने के लिए बढ़ने की आवश्यकता होगी।

यहाँ पूर्ण एक्सएमएल है (बटन ड्रॉएबल शामिल नहीं हैं, इसलिए आपका पता जोड़ने के लिए स्वतंत्र महसूस):

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

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dip" 
     android:orientation="horizontal" 
     android:layout_weight="1" > 

     <Button 
      android:layout_width="0dip" 
      android:layout_height="match_parent" 
      android:layout_weight="1" /> 

     <Button 
      android:layout_width="0dip" 
      android:layout_height="match_parent" 
      android:layout_weight="1"/> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dip" 
     android:orientation="horizontal" 
     android:layout_weight="1" > 

     <Button 
      android:layout_width="0dip" 
      android:layout_height="match_parent" 
      android:layout_weight="1" /> 

     <Button 
      android:layout_width="0dip" 
      android:layout_height="match_parent" 
      android:layout_weight="1"/> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dip" 
     android:orientation="horizontal" 
     android:layout_weight="1" > 

     <Button 
      android:layout_width="0dip" 
      android:layout_height="match_parent" 
      android:layout_weight="1" /> 

     <Button 
      android:layout_width="0dip" 
      android:layout_height="match_parent" 
      android:layout_weight="1" /> 
    </LinearLayout> 
</LinearLayout> 

और परिणाम: enter image description here

+0

मैंने अपने प्रश्न को एक्सएमएल कोड के साथ संपादित किया। लेकिन इस कोड के साथ मुझे स्क्रीन के प्रदर्शन के साथ समस्याएं हैं जो स्क्रीन – bisemanu

+0

'एंड्रॉइड पर सही ढंग से प्रदर्शित नहीं होती हैं: layout_width =" 0dip "' उन चीजों की सभी चौड़ाई के लिए आपको चाहिए। – Voicu

+0

मैंने अपना प्रश्न संपादित किया लेकिन – bisemanu

3

मुझे लगता है कि आप GridView

+0

विंडोज टाइल्स ने कोशिकाओं को भी विभाजित किया है, ग्रिड व्यू कैसे मदद करेगा? – Siddharth

0

उपयोग GridLayout! यह इस स्थिति के लिए सही है

<GridLayout 
    android:layout_width="match_parent" 
    android:layout_height="350dp" 
    android:layout_margin="0.5dp" 
    android:columnCount="2" 
    android:rowCount="3" > 

    <Button 
     android:id="@+id/b_1" 
     /> 

    <Button 
     android:id="@+id/b_2" 
     /> 

    <Button 
     android:id="@+id/b_3" 
     /> 

    <Button 
     android:id="@+id/b_4" 
     /> 

    <Button 
     android:id="@+id/b_5" 
     /> 

    <Button 
     android:id="@+id/b_6" 
     /> 
</GridLayout> 
0

मैं एंड्रॉइड .v7 पुस्तकालयों का उपयोग कर रहा हूं। इस एक्सएमएल काम किया मुझे 2 कॉलम बनाने के लिए, 3 पंक्तियों लेआउट है कि पूरे स्क्रीन पर आ जाएगा:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.GridLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/lib/android.support.v7.widget.GridLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    xmlns:grid="http://schemas.android.com/apk/res-auto" 
    android:layout_margin="0.5dp" 
    app:columnCount="2" 
    app:rowCount="3" 
    app:useDefaultMargins="true"> 

    <Button 
     android:id="@+id/b_1" 
     grid:layout_columnWeight="1" 
     grid:layout_rowWeight="1" 
     grid:layout_row="0" 
     grid:layout_column="0" 
     android:text="Hellooo"/> 

    <Button 
     android:id="@+id/b_2" 
     grid:layout_columnWeight="1" 
     grid:layout_rowWeight="1" 
     grid:layout_row="0" 
     grid:layout_column="1" 
     android:text="Hellooo"/> 

    <Button 
     android:id="@+id/b_3" 
     grid:layout_columnWeight="1" 
     grid:layout_rowWeight="1" 
     grid:layout_row="1" 
     grid:layout_column="0" 
     android:text="Hellooo"/> 

    <Button 
     android:id="@+id/b_4" 
     grid:layout_columnWeight="1" 
     grid:layout_rowWeight="1" 
     grid:layout_row="1" 
     grid:layout_column="1" 
     android:text="Hellooo"/> 

    <Button 
     android:id="@+id/b_5" 
     grid:layout_columnWeight="1" 
     grid:layout_rowWeight="1" 
     grid:layout_row="2" 
     grid:layout_column="0" 
     android:text="Hellooo"/> 

    <Button 
     android:id="@+id/b_6" 
     grid:layout_columnWeight="1" 
     grid:layout_rowWeight="1" 
     grid:layout_row="2" 
     grid:layout_column="1" 
     android:text="Hellooo"/> 

</android.support.v7.widget.GridLayout> 
संबंधित मुद्दे