如何在屏幕上显示新视图时调整视图大小/缩短?

时间:2017-07-25 17:56:02

标签: android google-maps android-layout android-fragments

我在屏幕上有两个视图,一个Google Map片段和一个不同的自定义片段。在自定义片段出现在屏幕上之前,Google地图是全屏的,这很好。一旦新片段出现在屏幕底部,我想要调整Google Map的大小,以便它只占用新片段未使用的可用空间。

即。在新片段之前

|----------------|
|                |
|                |
|                |
|                |
|      Map       |
|                |
|                |
|                |
|                |
|                |
|                |
|----------------|

在新片段之后

|----------------|
|                |
|                |
|                |
|      Map       |
|                |
|                |
|                |
|----------------| <-- Map ends here
|                |
|  New Fragment  |
|                |
|----------------|

XML文件:

<android.support.v4.widget.DrawerLayout
    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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout">

    <android.support.design.widget.NavigationView
        android:layout_width="200dp"
        android:layout_height="match_parent"
        app:menu="@menu/navigation_menu"
        android:id="@+id/navigation_menu"
        android:layout_gravity="start">

    </android.support.design.widget.NavigationView>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <fragment
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/map"
            android:name="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context="com.example.damia.placefinder.activities.MapsActivity" />

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:id="@+id/container_locations"
            android:layout_alignParentBottom="true"
            android:background="@color/cardview_light_background"
            android:visibility="gone">

        </FrameLayout>

    </RelativeLayout>

</android.support.v4.widget.DrawerLayout>

由于

1 个答案:

答案 0 :(得分:1)

现在是使用LinearLayoutlayout_weight属性的好时机。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        tools:context="com.example.damia.placefinder.activities.MapsActivity"/>

    <FrameLayout
        android:id="@+id/container_locations"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:background="@color/cardview_light_background"
        android:visibility="gone">

    </FrameLayout>

</LinearLayout>

如果出于某种原因LinearLayout冒犯了你的感情,你也可以ConstraintLayout来做。

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/container_locations"
        tools:context="com.example.damia.placefinder.activities.MapsActivity"/>

    <FrameLayout
        android:id="@+id/container_locations"
        android:layout_width="0dp"
        android:layout_height="300dp"
        android:background="@color/cardview_light_background"
        android:visibility="gone"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent">

    </FrameLayout>

</android.support.constraint.ConstraintLayout>
相关问题