地图片段几乎填满了所有的屏幕
我想分享我的屏幕布局在MapFragment
和简单的TextView
之间的高度,这样屏幕底部的小型TextView
就会占用所需的所有空间,而GoogleMap
片段填充剩余的可用屏幕。地图片段几乎填满了所有的屏幕
这样的:
唯一的问题是,我只能通过静态指定片段的android:layout_height
在450dp
取得这样的成绩,我还没有找到一种方法,这样做动态,这样布局就可以适应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>
,这是不想要的结果:
我的问题的意见解决了这个问题。
android:layout_weight
属性不起作用,只是因为将ScrollView
作为根标记来阻止其效果。 通过将RelativeLayout
作为根标签,weigth属性起作用,并且TextView应该在地图调整的同时向上扩展。
<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:weightSum
给LinearLayout
一个总重量为10.
所以fragment
得到9/10的LinearLayout,TextView
得到1/10的布局。
为什么你指定了两个属性'android:layout_weight = 9'和' android:layout_weight = 1'的片段? –
因为他们每个人都有自己的体重。一个是片段,一个是TextView。 –
我的意思是相同的片段视图。无法为同一视图放置两次相同的属性。 –
您可以使用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>
听起来好像你最适合使用'RelativeLayout'。将'TextView'指定到屏幕底部,以及其上方和顶部附近标签下方的地图。然后,'TextView'应该根据需要向上扩展,然后地图会调整。 –
为什么一切都包裹在ScrollView中? :| – Simas
你的意见解决了,谢谢。 'android:layout_weight'属性不工作只是因为'ScrollView'标签...可能你应该留下一个答案让我接受;) –