android - 几乎填满整个屏幕的 map fragment

标签 android android-layout android-fragments layout

我正在尝试在 MapFragment 和简单的 TextView 之间共享我的屏幕布局高度,以这样一种方式,小的 TextView 在屏幕底部占据了它需要的所有空间,而 GoogleMap fragment 填充了可用屏幕的其余部分。

像这样:

enter image description here

唯一的问题是我只能通过在 450dp 静态指定 fragment 的 android:layout_height 来获得这样的结果,而且我还没有找到方法动态地这样做,这样布局就会适应其横向模式和具有不同屏幕尺寸的移动设备。

这是我尝试做的:

<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

最佳答案

<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 - 几乎填满整个屏幕的 map fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30945671/

相关文章:

android webview 不加载移动版

java - 我应该如何在 Android 中给图像圆角?

android - 圆形 ImageView 显示方形图像

android - onMeasure() 以 EXACTLY 和规范大小 0 调用

android - 如何检测何时在 fragment android中按下后退按钮?

android - 致命异常:IllegalStateException无法执行 Activity 的方法

android - 当另一个滚动时如何隐藏回收 View

java - Fragment 未显示且 OnCreateView 方法未调用

android - 在位图上居中文本

android - 在 fragment 上输入 EditText 并填充所有其他 fragment