2013-06-04 63 views
0

如何在android中的線性佈局中將按鈕對齊到右側。 該按鈕僅粘貼在佈局中的textView旁邊。無法在xml中將按鈕對齊到右側

這是我的xml文件。

<LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal|top"> 

    <com.aavishkaar.quikies.widget.TypedfacedTextView 
      xmlns:your_namespace="http://schemas.android.com/apk/res/com.aavishkaar.qui" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:text="Bluetooth" 
      android:id="@+id/textView" 
      android:textSize="24sp" 
      android:textColor="@android:color/black" 
      android:gravity="center|left" 
      android:layout_marginLeft="15dp" 
      your_namespace:typeface="Roboto-Regular.ttf" 
      android:singleLine="false" 
      /> 

    <Button 
      style="?android:attr/buttonStyleSmall" 
      android:id="@+id/blue0" 
      android:layout_width="40dp" 
      android:layout_height="40dp" 
      android:text="OFF" 
      android:layout_gravity="center_vertical" 
      android:layout_marginTop="7dp" 
      android:layout_marginBottom="7dp" 
      android:background="@drawable/switch_bg_holo_light" 

      /> 

    <Button 
      style="?android:attr/buttonStyleSmall" 
      android:id="@+id/blue1" 
      android:layout_width="40dp" 
      android:layout_height="40dp" 
      android:text="ON" 
      android:layout_marginTop="7dp" 
      android:layout_marginBottom="7dp" 
      android:layout_marginRight="5dp" 
      android:background="@drawable/switch_thumb_activated_holo_light" 
      /> 
     </LinearLayout> 
+2

使用RelativeLayout的,而不是LinearLayout中。並添加一個toParentRight =「true」屬性到您的按鈕 – Arunkumar

+0

其實我有2個按鈕,並將它們對齊使它們重疊。如何解決這個問題.. – Vinuthan

回答

0

添加間隔觀點:

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_horizontal|top"> 

<com.aavishkaar.quikies.widget.TypedfacedTextView 
     xmlns:your_namespace="http://schemas.android.com/apk/res/com.aavishkaar.qui" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:text="Bluetooth" 
     android:id="@+id/textView" 
     android:textSize="24sp" 
     android:textColor="@android:color/black" 
     android:gravity="center|left" 
     android:layout_marginLeft="15dp" 
     your_namespace:typeface="Roboto-Regular.ttf" 
     android:singleLine="false" 
     /> 

<!-- ADD THIS BELOW --> 

<View 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" /> 


<Button 
     style="?android:attr/buttonStyleSmall" 
     android:id="@+id/blue0" 
     android:layout_width="40dp" 
     android:layout_height="40dp" 
     android:text="OFF" 
     android:layout_gravity="center_vertical" 
     android:layout_marginTop="7dp" 
     android:layout_marginBottom="7dp" 
     android:background="@drawable/switch_bg_holo_light" 

     /> 


<Button 
     style="?android:attr/buttonStyleSmall" 
     android:id="@+id/blue1" 
     android:layout_width="40dp" 
     android:layout_height="40dp" 
     android:text="ON" 
     android:layout_marginTop="7dp" 
     android:layout_marginBottom="7dp" 
     android:layout_marginRight="5dp" 
     android:background="@drawable/switch_thumb_activated_holo_light" 
     /> 
    </LinearLayout> 
+0

其實我也有一個textView,所以使用這種方法也會調整它。我使用relativeLayout得到了解決方案,謝謝。 :) – Vinuthan