安装 Android Studio 后如何修复“attribute android:layout_width, layout_height and textview not allowed here”的问题

4
我安装了Android Studio,创建了一个项目后,我的Main_Activity.xml文件出现了一些错误。
    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout 
       xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:app="http://schemas.android.com/apk/res-auto"
       xmlns:tools="http://schemas.android.com/tools"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

这里不允许使用这些属性;AndroidManifest.中也存在类似的错误。


2
你能添加activity_main.xml的完整代码吗? - Birju Vachhani
1
@M.Qasim - 请在此处分享完整的屏幕截图或XML代码。 - khushbu vadi
你能尝试清理并重新构建吗? - sasikumar
4个回答

4

对我来说,这解决了问题:

文件->与Gradle文件同步项目

(请注意,由于美国的制裁,我正在使用VPN)


我们都是政治的受害者,但实际上这并不是我们的事。 - AmerllicA

2

对我来说,我从Github下载的应用程序是使用Android SDK平台23构建的,而我没有在我的Android Studio安装中下载这个版本。

打开Gradle Scripts下拉菜单中的build.gradle文件(Module:app),该菜单在Android视图中(在Android Studio中)。

在此文件中,您将看到类似于以下内容:

android {
compileSdkVersion 23
buildToolsVersion '27.0.3'

这意味着我必须去顶部的菜单栏,进入“工具”选项,然后打开SDK管理器。在那里,我需要显示软件包详细信息,并找到版本为23的SDK并下载它。之后,你的应用程序应该可以正常工作。

-1

我刚刚遇到了这种问题,解决起来非常简单,你只需要点击菜单栏文件 -> 无效缓存 / 重启,然后重新启动Android Studio即可。


-1
我的问题是:“属性android:gravity在此处不允许
我通过关闭Android Studio来解决这个问题。当我重新启动它时,错误消失了。

这并不真正回答问题。如果您有不同的问题,可以点击提问进行提问。如果想在此问题获得新答案时得到通知,可以关注此问题。一旦您获得足够的声望,您还可以添加悬赏以吸引更多关注。- 来自审核 - Antoine
那不是问题中的错误。 - General Grievance

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接