2015-06-19 57 views
1

我想分享我的屏幕布局在MapFragment和简单的TextView之间的高度,这样屏幕底部的小型TextView就会占用所需的所有空间,而GoogleMap片段填充剩余的可用屏幕。地图片段几乎填满了所有的屏幕

这样的:

enter image description here

唯一的问题是,我只能通过静态指定片段的android:layout_height450dp取得这样的成绩,我还没有找到一种方法,这样做动态,这样布局就可以适应landscape模式,并适应不同屏幕尺寸的手机。

这是我一直试图做的事:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_height="match_parent" 
    android:layout_width="match_parent" tools:context="madapps.bicitourbo.DetailsFragmentTwo"> 

    <LinearLayout android:id="@+id/map" 
     android:layout_height="wrap_content" android:layout_width="match_parent" 
     android:orientation="vertical" 
     android:weightSum="10"> 
     <!--android:paddingRight="16dp"--> 
     <!--android:paddingLeft="16dp"--> 

     <fragment xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/mapFrag" 
      android:layout_height="0dp" 
      android:layout_width="match_parent" 
      android:layout_weight="9" 
      android:name="com.google.android.gms.maps.MapFragment"/> 

     <TextView android:id="@+id/headerTitle" 
      android:gravity="center_horizontal" 
      android:layout_height="0dp" 
      android:layout_width="match_parent" 
      android:text="Partenza da: Piazza Santo Stefano" 
      android:textSize="@dimen/textSizeBig" android:textColor="@color/textLightBlack" 
      android:layout_marginBottom="16dp" 
      android:layout_marginTop="16dp" 
      android:layout_weight="1"/> 
    </LinearLayout> 

</ScrollView> 

,这是不想要的结果:

enter image description here

+1

听起来好像你最适合使用'RelativeLayout'。将'TextView'指定到屏幕底部,以及其上方和顶部附近标签下方的地图。然后,'TextView'应该根据需要向上扩展,然后地图会调整。 –

+1

为什么一切都包裹在ScrollView中? :| – Simas

+0

你的意见解决了,谢谢。 'android:layout_weight'属性不工作只是因为'ScrollView'标签...可能你应该留下一个答案让我接受;) –

回答

0

我的问题的意见解决了这个问题。

android:layout_weight属性不起作用,只是因为将ScrollView作为根标记来阻止其效果。 通过将RelativeLayout作为根标签,weigth属性起作用,并且TextView应该在地图调整的同时向上扩展。

1
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_height="match_parent" 
android:layout_width="match_parent" tools:context="madapps.bicitourbo.DetailsFragmentTwo"> 

<LinearLayout android:id="@+id/map" 
    android:layout_height="match_parent" android:layout_width="match_parent" 
    android:orientation="vertical" 
    android:weightSum="10"> 
    <!--android:paddingRight="16dp"--> 
    <!--android:paddingLeft="16dp"--> 

    <fragment xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/mapFrag" 
     android:layout_height="0dp" 
     android:layout_width="match_parent" 
     android:layout_weight="9" 
     android:name="com.google.android.gms.maps.MapFragment"/> 

    <TextView android:id="@+id/headerTitle" 
     android:gravity="center_horizontal" 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     android:text="Partenza da: Piazza Santo Stefano" 
     android:textSize="@dimen/textSizeBig" android:textColor="@color/textLightBlack" 
     android:layout_marginBottom="16dp" 
     android:layout_marginTop="16dp" 
     android:layout_weight="1"/> 
</LinearLayout> 

android:weightSumLinearLayout一个总重量为10.

所以fragment得到9/10的LinearLayout,TextView得到1/10的布局。

+0

为什么你指定了两个属性'android:layout_weight = 9'和' android:layout_weight = 1'的片段? –

+0

因为他们每个人都有自己的体重。一个是片段,一个是TextView。 –

+0

我的意思是相同的片段视图。无法为同一视图放置两次相同的属性。 –

0

您可以使用RelativeLayout这个

<ScrollView 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" 
    tools:context="madapps.bicitourbo.DetailsFragmentTwo"> 

    <RelativeLayout 
     android:id="@+id/map" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 
     <!--android:paddingRight="16dp"--> 
     <!--android:paddingLeft="16dp"--> 

     <TextView 
      android:id="@+id/headerTitle" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:layout_marginBottom="16dp" 
      android:layout_marginTop="16dp" 
      android:gravity="center_horizontal" 
      android:text="Partenza da: Piazza Santo Stefano" 
      android:textColor="@color/textLightBlack" 
      android:textSize="@dimen/textSizeBig" /> 

     <fragment xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/mapFrag" 
      android:name="com.google.android.gms.maps.MapFragment" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_above="@id/headerTitle" /> 
    </RelativeLayout> 

</ScrollView>