2013-12-14 16 views
6

Mam liniowy układ pionowy w rodzicu. Następnie poziomy układ liniowy na dnie w tym 3 przyciskiJak wyrównać 3 przyciski w linii, Android?

Chcę 1st przycisk w skrajnej lewej stronie aktywności w dolnym 2. środkowy przycisk i 3 przycisk w skrajnej prawej stronie

Oto xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

    <ImageView 
    android:id="@+id/viewImageView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:contentDescription="@null" 
    android:src="@drawable/ic_launcher" /> 

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="bottom" > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button" /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button" /> 

    <Button 
     android:id="@+id/button3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="right" 
     android:layout_gravity="fill_vertical" 
     android:text="Button" /> 
</LinearLayout> 

Odpowiedz

11

Dla tych rodzaju wyrównania (jeden przycisk w skrajnej lewej rodzica, inny w centrum i jeszcze jeden w skrajnym prawym rogu układu rodzica) RelativeLayout będzie m ore poręczny niż LinearLayout .because, jeśli używasz RelativeLayout jako układu nadrzędnego dla przycisków można wykorzystać android:layout_alignParentLeft="true" dla jednej button chcesz wyrównać w ekstremalnych lewo, android:layout_centerInParent="true" wyrównać w centrum i android:layout_alignParentRight="true" aby wyrównać przycisk w rogu bocznym prawy.

Spróbuj ..

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

    <ImageView 
    android:id="@+id/viewImageView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:contentDescription="@null" 
    android:src="@drawable/ic_launcher" /> 

<RelativeLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="bottom" > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:text="Button" /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:text="Button" /> 

    <Button 
     android:id="@+id/button3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:text="Button" /> 
</RelativeLayout> 

</LinearLayout> 
+0

Thanks to działa na mnie – WISHY

+0

@Wishy Jeżeli moja odpowiedź pomogła Ci, pls zaakceptować. Przejrzyj także moją edycję, aby uzyskać więcej wyjaśnień. – Hariharan

4

Ustaw orientację poziomą ma

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

    <Button 
     android:id="@+id/button1" 
     android:layout_weight="1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button" /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_weight="1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button" /> 

    <Button 
     android:id="@+id/button3" 
     android:layout_weight="1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="right" 
     android:layout_gravity="fill_vertical" 
     android:text="Button" /> 
</LinearLayout> 

i wykorzystanie wagi do przycisków

Snap

enter image description here

1

tak można to zrobić stosując layout_weight=1

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

      <Button 
       android:id="@+id/button1" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:text="left " /> 

      <Button 
       android:id="@+id/button1" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:text="Center" /> 

      <Button 
       android:id="@+id/button1" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:text="Right" /> 

     </LinearLayout> 
    </RelativeLayout> 
Powiązane problemy