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

Android Studio运行XML布局提示LinearLayoutCompat未声明错误如何解决

问题原因

该错误是项目未引入AppCompat依赖导致编辑器无法识别androidx.appcompat.widget.LinearLayoutCompat控件,同时你提供的布局代码还存在几处属性拼写错误,也会触发编译失败,具体修复步骤如下:

修复步骤

  • 第一步:引入AppCompat依赖
    打开app模块对应的build.gradle文件,在dependencies代码块中添加如下依赖,之后同步项目即可:

    dependencies {
        // 版本可匹配你项目的compileSdk版本
        implementation 'androidx.appcompat:appcompat:1.6.1'
    }
    

    同步完成后LinearLayoutCompat的声明错误会自动消失。

  • 第二步:修正布局代码的拼写错误
    你提供的代码共有3处错误需要修改:

    1. 第一个TextView的android:padding_top、android:padding_bottom为错误写法,正确属性是android:paddingTop、android:paddingBottom
    2. 第二个TextView的高度属性写成了android:layuout_height,多输入了一个字母u,正确为android:layout_height
    3. 闭合标签</androidx.appcompat.widget.LinearLayoutCompat>放在了代码块外部,需要移到布局内容的最后位置

修正后的完整布局代码如下:

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="16dp"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="QUANTITY"
        android:paddingTop="16dp"
        android:paddingBottom="16dp"/>

    <TextView
        android:id="@+id/quantity_text-view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/black"
        android:text="0"
        android:textSize="16sp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="PRICE"
        android:paddingTop="16dp"
        android:paddingBottom="16dp"/>

    <TextView
        android:id="@+id/price_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0"
        android:textColor="@color/black"
        android:textSize="16sp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="3dp"
        android:layout_marginTop="16dp"
        android:text="ORDER"
        android:onClick="submitOrder"/>

</androidx.appcompat.widget.LinearLayoutCompat>

内容的提问来源于stack exchange,提问作者ARSALAN AHMAD 74

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 02:54:05