2017-08-20 6 views
-3

Ich habe drei Tasten müssen in einer Zeile am unteren Rand des Bildschirms befinden. Unten ist der Code in Aktivität xml. Um die drei Schaltflächen platzsparend zu machen, wickle ich sie jeweils in ein LinearLayout und setze das Layout android:layout_weight auf 1 und android:layout_gravity in die Mitte.Wie werden drei gleichmäßig unter LinearLayout angeordnete Schaltflächen angeordnet?

<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="90px" 
     android:background="#88104502" 
     android:orientation="horizontal"> 

     <LinearLayout 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_gravity="center" 
      android:orientation="horizontal" 
      android:layout_weight="1"> 
      <Button 
       android:id="@+id/goBack" 
       android:layout_width="70px" 
       android:layout_height="80px" 
       android:gravity="center" 
       android:background="@drawable/back" 
       android:onClick="toHomePage" /> 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_gravity="center" 
      android:orientation="horizontal" 
      android:layout_weight="1"> 
      <Button 
       android:id="@+id/findPlant" 
       android:layout_width="70px" 
       android:layout_height="80px" 
       android:gravity="center" 
       android:background="@drawable/flowr" 
       android:onClick="toImagePage" /> 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:orientation="horizontal" 
      android:layout_gravity="center" 
      android:layout_weight="1"> 

      <Button 
       android:id="@+id/plantList" 
       android:layout_width="70px" 
       android:layout_height="80px" 
       android:gravity="center" 
       android:background="@drawable/list" /> 
     </LinearLayout> 
    </LinearLayout> 

Aber es wird der Knopf nicht in der Mitte des Linearlayout entfernt. Unten ist der Screenshot:

enter image description here

Sie, dass jede Taste auf der linken Seite des Linearlayout befindet sich sehen können. Wie man sie zentriert macht?

+1

Warum verwenden Sie drei verschiedene lineare Layouts? Es wird auch mit einem HauptlinearLayout gemacht und gibt 3 Tasten gleiches Gewicht. –

Antwort

0

bereits. Ich verwende Ihren Code nur Schwerkraft auf lineare Layout.Wenn Sie nicht so viel von LinearLayout verwenden möchten, verwenden Sie nur eine Haupt LinearLayout und geben Sie gleich 3 Tasten.

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="90px" 
    android:background="#88104502" 
    android:orientation="horizontal"> 

<LinearLayout 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:layout_gravity="center" 
    android:orientation="horizontal" 
    android:gravity="center" 
    android:layout_weight="1"> 
    <Button 
     android:id="@+id/goBack" 
     android:layout_width="70px" 
     android:layout_height="80px" 
     android:gravity="center" 
     android:background="@mipmap/ic_launcher" 
     android:onClick="toHomePage" /> 
</LinearLayout> 

<LinearLayout 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:layout_gravity="center" 
    android:gravity="center" 
    android:orientation="horizontal" 
    android:layout_weight="1"> 
    <Button 
     android:id="@+id/findPlant" 
     android:layout_width="70px" 
     android:layout_height="80px" 
     android:gravity="center" 
     android:background="@mipmap/ic_launcher" 
     android:onClick="toImagePage" /> 
</LinearLayout> 

<LinearLayout 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" 
    android:layout_gravity="center" 
    android:gravity="center" 
    android:layout_weight="1"> 

    <Button 
     android:id="@+id/plantList" 
     android:layout_width="70px" 
     android:layout_height="80px" 
     android:background="@mipmap/ic_launcher" /> 
</LinearLayout> 

0

Sie können eine leere Ansicht als Padding erstellen. Auf diese Weise bleibt der Abstand immer gleich, auch wenn die Symbolbreiten unterschiedlich sind.

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="90px" 
    android:background="#88104502" 
    android:orientation="horizontal"> 

     <View 
      android:layout_weight="1"      
      android:layout_width="0dp" 
      android:layout_height="0dp"/> 

     <Button 
      android:id="@+id/button1" 
      android:layout_width="70px" 
      android:layout_height="80px" 
      android:background="@drawable/list" /> 
     <View 
      android:layout_weight="1"      
      android:layout_width="0dp" 
      android:layout_height="0dp"/> 

     <Button 
      android:id="@+id/button2" 
      android:layout_width="70px" 
      android:layout_height="80px" 
      android:background="@drawable/list" /> 
     <View 
      android:layout_weight="1"      
      android:layout_width="0dp" 
      android:layout_height="0dp"/> 

     <Button 
      android:id="@+id/button3" 
      android:layout_width="70px" 
      android:layout_height="80px" 
      android:background="@drawable/list" /> 
     <View 
      android:layout_weight="1"      
      android:layout_width="0dp" 
      android:layout_height="0dp"/> 
</LinearLayout> 
1

Wenn Sie auf eine Schaltfläche mit einem Bild möchten, können Sie Image anstelle von Button, und Sie können wie diese versuchen, und stattdessen verschachtelten Layout mit Ihnen direkt Gewicht zu Image geben kann.

Wenn Sie eine Schaltfläche stylen möchten, können Sie AppCompatImageButton darin verwenden, dass Sie app:backgroundTint verwenden können, um den Hintergrund zu ändern.

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="90px" 
    android:background="#88104502" 
    android:weightSum="3" 
    android:orientation="horizontal"> 

    <ImageButton 
     android:id="@+id/goBack" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:gravity="center" 
     android:onClick="toHomePage" 
     android:src="@drawable/ic_action_name"/> 

    <ImageButton 
     android:id="@+id/findPlant" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:gravity="center" 
     android:onClick="toImagePage" 
     android:src="@drawable/ic_action_name"/> 

    <ImageButton 
     android:id="@+id/plantList" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:gravity="center" 
     android:onClick="toImagePage" 
     android:src="@drawable/ic_action_name"/> 


</LinearLayout> 
+0

Dies ist das sauberste ** (und auch das ** schnellste **) Layout unter allen Antworten. Halten Sie es ** einfach: Leistungen ** sind relevant. +1 –

0

Verwenden der Schwerkraft statt layout_gravity.

Verwandte Themen