【问题标题】:How to make a view resize/shorten itself when a new view appears on screen?当屏幕上出现新视图时,如何使视图自行调整大小/缩短?
【发布时间】:2017-07-25 17:56:02
【问题描述】:

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

即。在新片段之前

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

谢谢

【问题讨论】:

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


    【解决方案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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-27
      • 2016-12-08
      • 1970-01-01
      • 1970-01-01
      • 2019-09-02
      相关资源
      最近更新 更多