MapFragment在单独的UI细分中使用TextView和Buttons

时间:2017-01-17 17:51:53

标签: android google-maps textview supportmapfragment

我遇到MapFragment的问题而且我不知道如何让它填满屏幕。我需要一个MapFragment来停止我的textview。我附上了一张更好地描述我需要它的图片。 现在我的MapFragment填充屏幕,textview放在MapFragment的顶部。

然后我有两个按钮位于我的textview之上。这没关系。

这就是我想要的样子:

enter image description here

希望你能帮忙!

这是布局:

<?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>

1 个答案:

答案 0 :(得分:0)

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

这比使用硬编码的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>

结果:

enter image description here