2017-06-18 104 views
-2

我如下三個按鈕放置,線性佈局水平,重力問題

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:layout_marginTop="8dp" 
     android:layout_marginBottom="8dp" 
     android:layout_gravity="center"> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Standard" 
      android:id="@+id/standard_map"/> 
     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="satellite" 
      android:id="@+id/satellite"/> 
     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Hybrid" 
      android:id="@+id/hybrid"/> 
    </LinearLayout> 

    <fragment 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/map" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:name="com.google.android.gms.maps.MapFragment" 
     map:cameraTargetLat="13.0827" 
     map:cameraTargetLng="80.2707" 
     map:cameraBearing="112.5" 
     map:cameraTilt="65" 
     map:cameraZoom="20"/> 

</LinearLayout> 

輸出收率如下: image explaining the problem

內線性佈局的取向是水平的,並且是完全精細。我需要按鈕在中心對齊(垂直居中)。即使當我使用android:gravity="centre"時,它也會將水平居中的按鈕對齊,這是無用的。 我需要按鈕在水平線性佈局中垂直居中。

回答

0

據我理解你的問題,你想在中心中間的按鈕 ,所以我也做了一些變化,請更新您的代碼。

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

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

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

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

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

    <fragment 
     android:id="@+id/map" 
     android:name="com.google.android.gms.maps.MapFragment" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     map:cameraBearing="112.5" 
     map:cameraTargetLat="13.0827" 
     map:cameraTargetLng="80.2707" 
     map:cameraTilt="65" 
     map:cameraZoom="20" /> 

</LinearLayout> 

PLZ讓我知道如果這個任何問題。

+0

謝謝..它的空間了等於..而且似乎集中..幹得好。 。 –

0

在第二個LinearLayout中更改layout_gravity和orientation屬性。所以,你的第二個佈局應該是這樣的:

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginBottom="8dp" 
    android:layout_marginTop="8dp" 
    android:gravity="center" 
    android:orientation="vertical"> 
... 
</LinearLayout> 

你會做到這一點:

enter image description here