2010-11-25 19 views
8

मैं एंड्रॉयड पर इन बटन कैसे केंद्रित कर सकता है? alt textकेंद्र लेआउट एंड्रॉयड

कोड मैं layout में उपयोग कर रहा हूँ है:

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

     <TextView android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/hello" /> 

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

      <Button android:text="Test" 
       android:layout_width="100px" 
       android:layout_height="40px" /> 
      <Button android:text="Test" 
       android:layout_width="100px" 
       android:layout_height="40px" /> 
      <Button android:text="Test" 
       android:layout_width="100px" 
       android:layout_height="40px" /> 
      <Button android:text="Test" 
       android:layout_width="100px" 
       android:layout_height="40px" /> 
     </LinearLayout> 
</LinearLayout> 

उत्तर

15

LinearLayout बटन युक्त चौड़ाई पर wrap_content करने और गुरुत्वाकर्षण पर center_horizontal के लिए सुनिश्चित करें।

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

<TextView android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/hello" /> 

<LinearLayout android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:orientation="horizontal" 
     android:layout_gravity="center_horizontal"> 
    <Button android:text="Test" 
     android:layout_width="100px" 
     android:layout_height="40px" /> 
    <Button android:text="Test" 
     android:layout_width="100px" 
     android:layout_height="40px" /> 
    <Button android:text="Test" 
     android:layout_width="100px" 
     android:layout_height="40px" /> 
    <Button android:text="Test" 
     android:layout_width="100px" 
     android:layout_height="40px" /> 
     </LinearLayout> 
</LinearLayout> 
+1

बहुत बढ़िया। धन्यवाद! –

+0

पूरी तरह से काम कर रहा है, धन्यवाद। –

1

मुझे पता है कि अगर किसी और को इस पर कोई टिप्पणी भी है चाहते हैं। मैंने हमेशा इसे टेबललाउट के साथ किया है और दोनों कॉलमों को कॉल किया है और सभी स्तंभों पर कॉलम कॉलम है, इसलिए यह सभी स्क्रीन आकारों पर स्केल करता है। इस तरह थोड़ा:

<TableLayout 
    android:layout_width="fill_parent" 
    android:layout_height="47dp" 
    android:stretchColumns="*" 
    android:collapseColumns="*" 
    android:padding="0px" 
    android:background="@drawable/bottom_bg"> 
    <TableRow> 
    <ImageView 
     android:id="@+id/article_button_home" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/button_home_selector" 
     android:layout_centerHorizontal="true" /> 
    <ImageView 
     android:id="@+id/article_button_share" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/button_sharearticle_selector" 
     android:layout_centerHorizontal="true" /> 
    <ImageView 
     android:id="@+id/article_button_settings" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/button_settings_selector" 
     android:layout_centerHorizontal="true"/> 
    <ImageView 
     android:id="@+id/article_button_about" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true"  
     android:src="@drawable/button_about_selector"/> 
</TableRow> 
</TableLayout> 

मुझे आश्चर्य है कि यह सबसे अच्छा तरीका है या नहीं।

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