Android开发:activity_maps.xml中按钮不显示问题求助
Hey there! Let's break down why your buttons aren't appearing as expected. Looking at your activity_maps.xml code snippet, the main issue is related to how your SupportMapFragment is sized within the vertical LinearLayout.
The Root Cause
Your SupportMapFragment has android:layout_height="match_parent" set, while its parent LinearLayout uses android:layout_height="wrap_content" and is oriented vertically. When you set a child view to match_parent in a vertical LinearLayout, that view will take up all available space in the parent container—pushing any subsequent views (like your buttons) completely off-screen. That's why you can't see them!
Fixes to Try
Here are two straightforward solutions to get your buttons visible again:
1. Use Layout Weight for Proportional Sizing (Recommended)
Change your parent LinearLayout to fill the entire screen, then use layout_weight to let the map take up most of the space while leaving room for your buttons. Here's how to adjust your code:
<LinearLayout 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:layout_height="match_parent" android:layout_width="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"/> <!-- Your button(s) go here, they'll sit below the map --> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Example Button"/> </LinearLayout>
By setting the map's height to 0dp and giving it a layout_weight="1", it will automatically expand to fill all space not used by your buttons. The parent LinearLayout uses match_parent to fill the screen, which makes this layout work correctly.
2. Set a Fixed Height for the Map
If you don't want the map to take up most of the screen, you can set a fixed height (like 300dp) for the fragment instead:
<fragment android:id="@+id/map" android:name="com.google.android.gms.maps.SupportMapFragment" android:layout_width="match_parent" android:layout_height="300dp"/>
This leaves space below the map for your buttons to be visible, as long as the parent LinearLayout has enough space (you might still want to set the parent's height to match_parent here too).
Quick Check
Double-check that your buttons are placed after the fragment tag in the LinearLayout—if they were accidentally inside the fragment (which isn't allowed), they wouldn't render at all. But from your code snippet, it looks like they're separate, so the sizing issue is the main problem.
内容的提问来源于stack exchange,提问作者user9537534

