带有 TextView 和按钮的 MapFragment 在单独的 UI 段中

MapFragment with TextView and Buttons in separate UI segment

我的 MapFragment 有问题,我不知道如何让它不填满屏幕。我需要有一个 MapFragment 来停在我的文本视图上。我附上一张图片,可以更好地描述我需要它的外观。 现在我的 MapFragment 填满了屏幕,textview 被放置在 MapFragment 的顶部。

然后我的文本视图顶部有两个按钮。这个可以。

这就是我想要的样子:

希望能帮到你!

布局如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_alignBottom="@id/activity_main"
    android:layout_width="match_parent"
    android:weightSum="10"
    android:keepScreenOn="true"
    android:layout_height="match_parent">


    <LinearLayout
        android:id="@+id/linelay"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <fragment android:name="com.google.android.gms.maps.SupportMapFragment"
            android:id="@+id/map"
            android:layout_width="match_parent"
            android:layout_weight="9"
            android:layout_height="wrap_content"
            tools:context="com.example.MapsActivity"
            tools:layout="@layout/activity_main"/>


    </LinearLayout>

    <ToggleButton
        android:text="ToggleButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:id="@+id/toggleButton" />

    <Button
        android:text="@string/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/toggleButton"
        android:layout_alignParentRight="true"
        android:onClick="clearMap"
        android:id="@+id/button1" />


    <TextView
        android:id="@+id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Hello World!"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentBottom="true"
        android:background="@drawable/frame" />

</RelativeLayout>

这是一种方法。使用一个LinearLayout作为外层Layout,并使用布局权重来定义不同部分的高度。

这比使用 hard-coded dp 值定义任一部分的高度要好。对高度使用布局权重将更好地动态调整不同的屏幕尺寸。

<LinearLayout 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="com.danielnugent.mapapplication.MapsActivity"
    android:orientation="vertical"
    android:weightSum="1">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="vertical"
        android:layout_weight=".8">
        <fragment
            android:id="@+id/map"
            class="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </LinearLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="vertical"
        android:layout_weight=".2">

        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView Text Here"
            android:layout_marginLeft="40dp"
            android:layout_marginStart="40dp"
            android:lineSpacingExtra="14sp"
            android:textSize="18sp"
            android:layout_above="@+id/button1"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 1"
            android:layout_marginRight="18dp"
            android:layout_marginEnd="18dp"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_above="@+id/button1" />

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 2"
            android:layout_alignParentBottom="true"
            android:layout_alignLeft="@+id/button2"
            android:layout_alignStart="@+id/button2" />

    </RelativeLayout>
</LinearLayout>

结果: