Android 的 xml 中一个 <View> 如果被设置了 android:visibility="gone" 之后,代码中还能通过 findbyId 方式获取到吗
时间: 2024-04-19 19:26:26 浏览: 8
是的,即使一个 `<View>` 在 XML 中设置了 `android:visibility="gone"`,它仍然可以在代码中通过 `findViewById` 方法找到。`findViewById` 方法会返回一个对应给定 ID 的 View 对象,无论其在布局中是否可见。但是需要注意的是,如果一个 View 在 XML 中设置了 `android:visibility="gone"`,则在布局中是不可见的,无法响应用户的交互事件。
相关问题
android:visibility="GONE"
在Android开发中,android:visibility="GONE"是一种视图的可见性属性。当一个视图的可见性设置为GONE时,该视图将不会在屏幕上显示,并且不会占用任何空间。
具体来说,当一个视图的可见性设置为GONE时,它将会被隐藏,并且不会对布局产生任何影响。相比之下,当可见性设置为INVISIBLE时,视图也会被隐藏,但是它仍然会占用布局空间。
使用android:visibility="GONE"可以在需要时动态地隐藏或显示视图。这在处理动态布局或根据特定条件显示或隐藏视图时非常有用。
下面是一些相关问题:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/poi_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:baselineAligned="false" android:orientation="horizontal"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:id="@+id/poi_list_container" android:layout_width="360dp" android:layout_height="match_parent" android:layout_above="@id/bmapView" android:background="@drawable/bg_list_circular" android:visibility="gone"> <ListView android:id="@+id/poi_list" android:layout_width="match_parent" android:layout_height="match_parent" android:hardwareAccelerated="true" android:scrollbars="none" /> </RelativeLayout> <!-- <RelativeLayout--> <!-- android:layout_width="0dp"--> <!-- android:layout_height="match_parent"--> <!-- android:layout_weight="3">--> <com.baidu.mapapi.map.MapView android:id="@+id/bmapView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentTop="true" android:clickable="true" android:focusable="true" android:hardwareAccelerated="true" /> </RelativeLayout> </LinearLayout> 这样写的话显示不出来listview,我想要的结果是 将RelativeLayout包裹的listview放到mapview之后,并且显示在布局的左侧,不占用mapview的空间
非常抱歉,我之前的回答仍然有误。您可以尝试以下代码来实现您想要的效果:
```
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/poi_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<RelativeLayout
android:id="@+id/poi_list_container"
android:layout_width="360dp"
android:layout_height="match_parent"
android:background="@drawable/bg_list_circular"
android:visibility="gone">
<ListView
android:id="@+id/poi_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hardwareAccelerated="true"
android:scrollbars="none" />
</RelativeLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.baidu.mapapi.map.MapView
android:id="@+id/bmapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusable="true"
android:hardwareAccelerated="true" />
<RelativeLayout
android:layout_width="360dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="@android:color/transparent">
<!-- Add any other views you want to display here -->
</RelativeLayout>
</FrameLayout>
</LinearLayout>
```
这个布局将ListView包裹在RelativeLayout中,并在左侧显示,不占用MapView的空间。通过将RelativeLayout放置在FrameLayout中,可以将其叠加在MapView上,从而实现您想要的效果。