2015-06-19 65 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>