You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 15:10:39