2017-06-18 187 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