【问题标题】:MapView being shrunken when adding buttons to a MapView fragment向 MapView 片段添加按钮时 MapView 被缩小
【发布时间】:2019-03-15 13:58:55
【问题描述】:

我正在尝试创建一个地图应用程序,它允许您使用两个按钮在地图和卫星视图之间切换。但是,当我添加这两个按钮时,地图会缩小到不占用设备的全屏。

Shrunken Map

如何使地图占据整个屏幕,然后向下滚动时按钮位于该屏幕下方?这是布局文件的代码:

<?xml version="1.0" encoding="utf-8"?>



<ScrollView android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    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"
    map:uiZoomControls="true"
    tools:context=".MapsActivity" />


   <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Map View"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Satellite View"/>

   </LinearLayout>

</LinearLayout>


</ScrollView>

【问题讨论】:

    标签: android xml google-maps android-layout mapfragment


    【解决方案1】:

    你好,试试 FrameLayout 而不是 LinearLayout

    <ScrollView android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        xmlns:android="http://schemas.android.com/apk/res/android">
    
           <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
    
             <fragment xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:map="http://schemas.android.com/apk/res-auto"
              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"
              map:uiZoomControls="true"
              tools:context=".MapsActivity" />
    
    
       <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Map View"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Satellite View"/>
    
        </LinearLayout>
    
     </FrameLayout>
    
    
    </ScrollView>
    

    【讨论】:

      【解决方案2】:

      对于这个简单的布局,我不明白你为什么需要scrollView,你可以使用constarintLayout

      <?xml version="1.0" encoding="utf-8"?>
      <androidx.constraintlayout.widget.ConstraintLayout 
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      android:id="@+id/frameLayout2"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:context=".Fragments.MenusDesign.ExpandableCategoriesMenu.ExpandableCategoriesMenu">
      
      
      <fragment xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:map="http://schemas.android.com/apk/res-auto"
          xmlns:tools="http://schemas.android.com/tools"
          android:id="@+id/map"
          android:layout_width="0dp"
          android:layout_height="0dp"
          android:layout_marginStart="8dp"
          android:layout_marginTop="8dp"
          android:layout_marginEnd="8dp"
          android:layout_marginBottom="8dp"
          map:layout_constraintBottom_toTopOf="@+id/button"
          map:layout_constraintEnd_toEndOf="parent"
          map:layout_constraintStart_toStartOf="parent"
          map:layout_constraintTop_toTopOf="parent"
          map:uiZoomControls="true"
          tools:context=".MapsActivity" />
      
      
      <Button
          android:id="@+id/button"
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:layout_marginStart="8dp"
          android:layout_marginEnd="8dp"
          android:layout_marginBottom="8dp"
          android:text="Map View"
          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintStart_toStartOf="@+id/guideline2" />
      
      <Button
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:layout_marginStart="8dp"
          android:layout_marginEnd="8dp"
          android:layout_marginBottom="8dp"
          android:text="Satellite View"
          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintEnd_toStartOf="@+id/guideline2"
          app:layout_constraintStart_toStartOf="parent" />
      
      <androidx.constraintlayout.widget.Guideline
          android:id="@+id/guideline2"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:orientation="vertical"
          app:layout_constraintGuide_percent="0.5"/>
      

      【讨论】:

        猜你喜欢
        • 2013-02-17
        • 1970-01-01
        • 2011-10-14
        • 1970-01-01
        • 2016-06-13
        • 1970-01-01
        • 1970-01-01
        • 2014-11-28
        • 2016-04-18
        相关资源
        最近更新 更多