2016-10-15 6 views
0

Ich habe zwei lineare Layouts ineinander. In der zweiten habe ich eine Switch Taste, eine TextView und eine FloatingActionButton. Ich möchte diese 3 in die gleiche Zeile setzen und ich möchte den Switch und die TextView zentralisieren und ich möchte die FloatingActionButton auf der rechten Seite des Bildschirms ausrichten. Ich habe versucht, marginLeft zu verwenden, um sie zu zwingen, zu der Position zu gehen, die ich will, aber ich glaube, dass das nicht der richtige Weg ist.Android: Zentrum und rechts Ausrichtung innerhalb LinearLayout

mein Code hier (jetzt die 3-Komponenten sind bereits in der gleichen Zeile):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:fab="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="80dp" 
     android:orientation="horizontal" 
     android:weightSum="1" > 

    <Switch 
     android:id="@+id/mySwitch" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_marginLeft="140dp" 
     android:layout_marginTop="20dp" 
     android:theme="@style/SCBSwitch" 
     /> 

     <TextView 
      android:id="@+id/OpenClose" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="20dp" 
      android:layout_marginLeft="10dp" 
      android:text="Open" 
      android:textSize="20dp"/> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/action_find_location" 
     android:layout_width="277dp" 
     android:layout_height="40dp" 
     android:layout_marginTop="10dp" 
     android:layout_marginLeft="75dp" 
     android:layout_marginRight="10dp" 
     android:src="@drawable/calendar_today" 
     fab:backgroundTint="@color/theme_color" 
     fab:borderWidth="0dp" 
     fab:elevation="9dp" 
     fab:rippleColor="@color/toolbar_title_color" 
     fab:fabSize="mini"/> 

    </LinearLayout> 
    </LinearLayout> 
+0

haben Sie versuch android: layout_gravity = "left"; android: layout_gravity = "zentrum"; android: layout_gravity = "right" für jede Steuerung bzw. – user3501749

+0

Danke für die Antwort! Ja, ich habe es versucht, aber es hat nicht funktioniert. Ich weiß nicht, ob es wegen eines der anderen Attribute nicht funktioniert. –

Antwort

0

nahm einfach Ihren Code und einige Änderungen vorgenommen. Bitte überprüfen Sie, ob es gut funktioniert

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:fab="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical"> 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="80dp" 
    android:orientation="horizontal" 
    android:weightSum="3"> 

    <Switch 
     android:id="@+id/mySwitch" 
     android:layout_width="50dp" 
     android:layout_height="50dp" 
     android:theme="@style/SCBSwitch" 
     android:layout_gravity="start" 
     android:gravity="center" 
     android:layout_weight="1"/> 

    <TextView 
     android:id="@+id/OpenClose" 
     android:layout_width="50dp" 
     android:layout_height="50dp" 
     android:text="Open" 
     android:textSize="20dp" 
     android:layout_gravity="center_horizontal" 
     android:gravity="center" 
     android:layout_weight="1"/> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/action_find_location" 
     android:layout_width="50dp" 
     android:layout_height="50dp" 
     android:src="@drawable/calendar_today" 
     fab:backgroundTint="@color/theme_color" 
     fab:borderWidth="0dp" 
     fab:elevation="9dp" 
     fab:rippleColor="@color/toolbar_title_color" 
     fab:fabSize="mini" 
     android:layout_gravity="end" 
     android:gravity="center" 
     android:layout_weight="1"/> 

</LinearLayout> 
</LinearLayout> 
+0

Es hat funktioniert! Vielen Dank! –

+0

prost (y) @MarcosGuimaraes – user3501749

0

android:weightSum="1" vom LinearLayout löschen und so etwas tun:

<FrameLayout 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:layout_weight="1" 
    android:orientation="horizontal"> 

    <Switch 
     android:id="@+id/mySwitch" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" /> 
</FrameLayout> 

gleich dem TextView, und mit dem FAB löschen:

android:layout_gravity="end" 
    android:gravity="center" 
    android:layout_weight="1" 
Verwandte Themen