2012-12-22 23 views
11

मैं नीचे दिखाए गए डेटा को प्रदर्शित करने के लिए टेबल लेआउट का उपयोग कर रहा हूं। enter image description hereएंड्रॉइड टेबल लेआउट संरेखण

मैं क्या करना चाहता हूं?

मैं दूसरे कॉलम में पाठ को बाईं ओर गठबंधन करना चाहता हूं और पाठ को लपेटना चाहिए और अगली पंक्ति में प्रदर्शित होना चाहिए और जैसा कि आप छवि में देखते हैं।

कोड:

<TableLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/tableLayout1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 

     <TableRow 
      android:id="@+id/tableRow2" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:paddingBottom="10dp" > 

      <ImageView 
       android:id="@+id/place_category_icon" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerVertical="true" 
       android:contentDescription="ss" 
       android:paddingRight="10dp" 
       android:src="@drawable/icon_category" 
       android:textAlignment="textStart" /> 

      <TextView 
       android:id="@+id/textView2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerVertical="true" 
       android:layout_gravity="center" 
       android:text="230 kms" 
       android:textSize="16sp" > 
      </TextView> 
     </TableRow> 

     <TableRow 
      android:id="@+id/tableRow2" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:paddingBottom="10dp" > 

      <ImageView 
       android:id="@+id/place_category_icon" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerVertical="true" 
       android:contentDescription="ss" 
       android:paddingRight="10dp" 
       android:src="@drawable/icon_category" 
       android:textAlignment="textStart" /> 

      <TextView 
       android:id="@+id/textView2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerVertical="true" 
       android:layout_gravity="center" 
       android:text="Hill Station, Wild Life" 
       android:textSize="16sp" > 
      </TextView> 
     </TableRow> 

     <TableRow 
      android:id="@+id/tableRow2" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:paddingBottom="10dp" > 

      <ImageView 
       android:id="@+id/place_category_icon" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerVertical="true" 
       android:contentDescription="ss" 
       android:paddingRight="10dp" 
       android:src="@drawable/icon_category" 
       android:textAlignment="textStart" /> 

      <TextView 
       android:id="@+id/textView2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerVertical="true" 
       android:layout_gravity="center" 
       android:text="Summer 23-40°C, Winter 10-32°C" 
       android:textSize="16sp" > 
      </TextView> 
     </TableRow> 

     <TableRow 
      android:id="@+id/tableRow2" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:paddingBottom="10dp" > 

      <ImageView 
       android:id="@+id/place_category_icon" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerVertical="true" 
       android:contentDescription="ss" 
       android:paddingRight="10dp" 
       android:src="@drawable/icon_category" 
       android:textAlignment="textStart" /> 

      <TextView 
       android:id="@+id/textView2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerVertical="true" 
       android:layout_gravity="center" 
       android:text="Tippus Drop, Tippus Summer Residence, Brahmashram, Cycling, Paragliding" 
       android:textSize="16sp" > 
      </TextView> 
     </TableRow> 
    </TableLayout> 

यह कैसा

enter image description here

+0

टेक्स्टव्यू पैडिंग करने का प्रयास करें? – Dinesh

+0

@ दिनेश हाँ मैंने कोशिश की। वास्तव में –

+0

की सहायता नहीं करता है, जिसका मतलब है कि आप बाएं तरफ 230km चाहते हैं –

उत्तर

1

मैं सबसे आसान तरीका टी कर सकते हैं hink का एक LinearLayout इस तरह के साथ प्रत्येक TableRow सामग्री रैप करने के लिए है:

<TableLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/tableLayout1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 

     <TableRow 
      android:id="@+id/tableRow2" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:paddingBottom="10dp" > 

      <LinearLayout 
       android:layout_width="wrap_content" 
       android:layout_height="match_parent" > 

       <ImageView 
        android:id="@+id/place_category_icon" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_centerVertical="true" 
        android:contentDescription="ss" 
        android:paddingRight="10dp" 
        android:src="@drawable/ic_launcher" /> 

       <TextView 
        android:id="@+id/textView2" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_centerVertical="true" 
        android:layout_gravity="center" 
        android:text="230 kms" 
        android:textSize="16sp" > 
       </TextView> 
      </LinearLayout> 
     </TableRow> 

     <TableRow 
      android:id="@+id/tableRow2" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:paddingBottom="10dp" > 

      <LinearLayout 
       android:layout_width="wrap_content" 
       android:layout_height="match_parent" > 

       <ImageView 
        android:id="@+id/place_category_icon" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_centerVertical="true" 
        android:contentDescription="ss" 
        android:paddingRight="10dp" 
        android:src="@drawable/ic_launcher" /> 

       <TextView 
        android:id="@+id/textView2" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_centerVertical="true" 
        android:layout_gravity="center" 
        android:text="Hill Station, Wild Life" 
        android:textSize="16sp" > 
       </TextView> 
      </LinearLayout> 
     </TableRow> 

     <TableRow 
      android:id="@+id/tableRow2" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:paddingBottom="10dp" > 

      <LinearLayout 
       android:layout_width="wrap_content" 
       android:layout_height="match_parent" > 

       <ImageView 
        android:id="@+id/place_category_icon" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_centerVertical="true" 
        android:contentDescription="ss" 
        android:paddingRight="10dp" 
        android:src="@drawable/ic_launcher" /> 

       <TextView 
        android:id="@+id/textView2" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_centerVertical="true" 
        android:layout_gravity="center" 
        android:text="Summer 23-40°C, Winter 10-32°C" 
        android:textSize="16sp" > 
       </TextView> 
      </LinearLayout> 
     </TableRow> 

     <TableRow 
      android:id="@+id/tableRow2" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:paddingBottom="10dp" > 

      <LinearLayout 
       android:layout_width="wrap_content" 
       android:layout_height="match_parent" > 

       <ImageView 
        android:id="@+id/place_category_icon" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_centerVertical="true" 
        android:contentDescription="ss" 
        android:paddingRight="10dp" 
        android:src="@drawable/ic_launcher" /> 

       <TextView 
        android:id="@+id/textView2" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_centerVertical="true" 
        android:layout_gravity="center" 
        android:text="Tippus Drop, Tippus Summer Residence, Brahmashram, Cycling, Paragliding" 
        android:textSize="16sp" > 
       </TextView> 
      </LinearLayout> 
     </TableRow> 
    </TableLayout> 

और यह परिणाम है:

layout

आशा मैं सही ढंग से अपनी आवश्यकताओं को समझा।

+0

धन्यवाद। क्या पंक्तियों का अंतिम सेट बनाने के लिए वैसे भी है। केंद्र में शुरू करने के लिए और फिर नीचे प्रवाह करें? मैंने यह दिखाने के लिए अपना उत्तर अपडेट किया है कि मैं –

+1

प्राप्त करना चाहता हूं यदि आप टेक्स्टव्यू में पैडिंग टॉप जोड़ते हैं तो आप इसे प्राप्त कर सकते हैं। मुझे यकीन नहीं है कि ऐसा करने का एक और अधिक प्रभावी तरीका है। – Andrei

1

मेरा अनुमान दिखना चाहिए - layout_width बदल सकते हैं और गुरुत्वाकर्षण से छुटकारा पाने:

<TextView 
      android:id="@+id/textView2" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_centerVertical="true" 
      ... 
      > 
     </TextView> 
+0

पर लपेटने के लिए यह मदद नहीं करता है :( –

2

एक TableRow के बाद से काफी एक क्षैतिज LinearLayout उन्मुख है, बस बनाने के लिए TextViewImageView के बाद सभी जगह को भरने के वजन के सिद्धांतों का उपयोग करें। इसका मतलब है कि आप कुछ हद तक करने के लिए इस तरह पंक्तियों को बदलने के लिए होगा:

<!-- no need to set width/height as those are implicitly enforced --> 
    <TableRow 
     android:id="@+id/tableRow2" 
     android:paddingBottom="10dp" > 

     <ImageView 
      android:id="@+id/place_category_icon" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:contentDescription="ss" 
      android:paddingRight="10dp" 
      android:src="@drawable/icon_category" /> 

     <!-- width of '0' and weight of '1' will make this view fill up all remaining space --> 
     <TextView 
      android:id="@+id/textView2" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:layout_gravity="center_vertical" 
      android:text="230 kms" 
      android:textSize="16sp" /> 

    </TableRow> 

मैं भी हटा दिया है/कुछ ज़रूरत से ज़्यादा गुण बदल दिया है। ध्यान रखें कि मैंने इसे सीधे ब्राउज़र में टाइप किया है - अजीब टाइपो या ट्वीक आवश्यक हो सकता है।

+0

आपने एंड्रॉइड प्रदान किया है: layout_width = "0dp" यह शून्य क्यों है? –

+3

यह सामान्य अभ्यास है वजन से निपटने और 'लीनियरलाउट', क्योंकि यह माप/लेआउट प्रक्रिया को गति देता है। इस परिदृश्य में आपको प्रारंभिक चौड़ाई की परवाह नहीं है, बल्कि बच्चे के विचारों के बीच शेष स्थान कैसे वितरित किया जाता है। इस विशिष्ट मामले के लिए 'wrap_content' के साथ भी ठीक काम करेगा; यह थोड़ा सा कुशल है। –

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