首页 > 解决方案 > 当我尝试在 xml 文件中设置布局宽度和高度时出现错误

问题描述

设置布局高度和宽度时,android studio 显示该属性的错误

android:layout_heightandroid:layout_width分别在这里是不允许的。

并且设置TextView也显示错误:-

此处不允许使用元素 TextView。

我已阅读并尝试了Element LinearLayout is not allowed in CardView的答案, 但没有帮助。

我检查了是否有任何额外的左括号或右括号,但没有。gradle.build 文件有问题吗?我已经检查过,但看起来不错。

这是 activity__main.xml 文件中的代码。

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="16dp"
        android:textSize="20sp" />

</FrameLayout>

这是 build.gradle 的代码

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25

    defaultConfig {
        applicationId "com.android.example.favoritetoys"
        minSdkVersion 10
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:25.1.0'
}

标签: javaandroidandroid-studio

解决方案


推荐阅读