【问题标题】:Map fragment filling almost all the screen地图片段几乎填满了整个屏幕
【发布时间】:2015-09-05 21:08:50
【问题描述】:

我试图在MapFragment 和一个简单的TextView 之间共享我的屏幕布局的高度,这样屏幕底部的小TextView 就占据了它需要的所有空间,而GoogleMap 片段填充了可用屏幕的其余部分。

像这样:

唯一的问题是我只能通过在450dp处静态指定片段的android:layout_height来获得这样的结果,而我还没有找到动态这样做的方法,因此布局会同时适应它的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>

这是不想要的结果:

【问题讨论】:

  • 听起来好像您最适合使用RelativeLayout。将TextView 分配到屏幕底部,并将地图分配到它上方和靠近顶部的选项卡下方。然后TextView 应该会根据需要向上扩展并且地图会调整。
  • 为什么所有的东西都包裹在一个 ScrollView 中? :|
  • 您的 cmets 解决了,谢谢。 android:layout_weight 属性不起作用仅仅是因为 ScrollView 标签......也许你应该留下一个答案让我接受;)

标签: android android-layout android-fragments layout


【解决方案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 获得了 LinearLayout 的 9/10,TextView 获得了布局的 1/10。

【讨论】:

  • 为什么要为片段指定两个属性android:layout_weight=9android:layout_weight=1
  • 因为他们每个人都有自己的体重。一个用于片段,一个用于 TextView。
  • 我的意思是同一个片段视图。不能为同一个 View 放置两次相同的属性。
  • 同一个属性的两倍是什么意思?
  • 检查你的片段属性...你把属性android:layout_weight的两倍
【解决方案2】:

您可以为此使用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>

【讨论】:

    【解决方案3】:

    我的问题的 cmets 解决了它。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-06
      相关资源
      最近更新 更多