Android Studio布局文件中<space>标签被标记为错误的问题求助
解决Android Studio中标签报错的问题
Android布局XML里没有小写的<space>标签,这就是它标红报错的核心原因。你需要用官方提供的Space组件,注意首字母大写,并且使用正确的全限定类名:
- 若使用AndroidX库,标签写为
<androidx.core.widget.Space> - 若使用旧版支持库,标签写为
<android.widget.Space>
给你一个实际布局示例参考:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello"/> <!-- 正确的Space用法,添加水平间距 --> <androidx.core.widget.Space android:layout_width="24dp" android:layout_height="match_parent"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="World"/> </LinearLayout>
另外,如果是在Jetpack Compose中想添加间距,不能用XML标签,而是用Spacer函数,示例代码如下:
Row { Text("Hello") Spacer(modifier = Modifier.width(24.dp)) Text("World") }
替换成正确的组件写法后,报错就会消失。
内容的提问来源于stack exchange,提问作者JoergS
相关产品推荐
相关产品推荐

