在Support Map Fragment上添加EditText与TextView不显示问题求助
解决SupportMapFragment上EditText/TextView不显示的问题
嘿,我碰到过好几次类似的问题,大概率是布局层级或者顺序的锅!结合你给出的部分XML代码,我给你列几个最常见的原因和解决办法:
1. 布局声明顺序搞反了
RelativeLayout里的控件是按声明顺序堆叠的——后写的控件会盖在先写的上面。如果你的SupportMapFragment是在EditText/TextView之后声明的,那地图直接就把这两个控件挡住了,自然看不到。
解决办法很简单:把地图Fragment放在前面,EditText和TextView放在后面,比如这样写:
<RelativeLayout 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_width="match_parent" android:layout_height="match_parent"> <!-- 先放地图,它会占满整个布局 --> <fragment android:id="@+id/map_fragment" android:name="com.google.android.gms.maps.SupportMapFragment" android:layout_width="match_parent" android:layout_height="match_parent"/> <!-- 再放需要显示的EditText,它会在地图上层 --> <EditText android:id="@+id/input_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="16dp" android:hint="请输入内容" android:background="#fff"/> <!-- 同理,TextView放在后面也会显示在地图上 --> <TextView android:id="@+id/tv_info" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/input_text" android:layout_margin="16dp" android:text="这里是地图提示" android:textColor="#000" android:textSize="16sp" android:background="#fff"/> </RelativeLayout>
2. 不小心设置了隐藏属性
检查下你的EditText和TextView有没有被设置成android:visibility="gone"或者android:visibility="invisible",这俩属性一个是直接隐藏(不占布局空间),一个是透明(占空间但看不见)。默认是visible,如果之前改了,改回来就行。
3. 布局参数没设置对
比如如果你的EditText的layout_width或layout_height设成了0dp,或者在RelativeLayout里没给它设置定位(比如layout_alignParentTop、layout_centerHorizontal这类),可能会导致控件被挤到屏幕外面或者缩成一点,看起来就像没显示。
4. 给控件提升层级(绝招)
如果上面的方法都不管用,试试给EditText和TextView设置elevation属性,强制提升它们的层级,让它们稳稳地趴在地图上面:
<EditText android:id="@+id/input_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="16dp" android:hint="请输入内容" android:background="#fff" android:elevation="8dp"/> <!-- 这个值可以根据需要调整,越大层级越高 -->
要是你能把完整的XML代码贴出来,我还能更精准地帮你排查,但先试试上面这几个方案,应该能解决问题~
内容的提问来源于stack exchange,提问作者john
相关产品推荐
相关产品推荐

