解决Android布局中'margin'属性无资源标识符的编译错误
解决Android布局中
No resource identifier found for attribute 'margin'的问题 嘿,这个坑我之前也踩过!你遇到的错误核心原因非常明确:Android SDK里从来没有android:margin这个属性,你应该用的是android:layout_margin(如果需要单独设置某一边的边距,还有layout_marginTop、layout_marginStart这类细分属性)。
另外还要提一句:尽量用dp替代px作为单位,px是固定像素值,在不同分辨率的设备上显示效果会走形;而dp是密度无关像素,能保证在各种设备上的显示比例一致。
现在把你代码里所有错误的android:margin替换成android:layout_margin,同时把部分px改成dp,修改后的完整布局代码如下:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:padding="10dp" android:fillViewport="false"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="200dp" android:scaleType="centerCrop" android:background="@drawable/rp" android:src="@drawable/gradient" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#ffffff" android:layout_alignParentBottom="true" android:layout_margin="10dp" <!-- 替换margin为layout_margin,px换dp --> android:elevation="9dp" <!-- elevation用dp更合适 --> android:paddingHorizontal="20dp" android:textSize="60sp" <!-- 字体大小推荐用sp,适配系统字体缩放 --> android:textStyle="bold" android:text="Royal Physicsit of CDP" /> </RelativeLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:weightSum="2" android:layout_margin="2dp"> <!-- 替换margin为layout_margin,px换dp --> <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_margin="10dp" android:layout_height="wrap_content" android:background="@drawable/buttonbackground" android:textColor="#ffffff" <!-- 删掉重复的android:margin="10px",保留layout_margin即可 --> android:drawableLeft="@drawable/join" android:paddingHorizontal="20dp" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Join Us" android:layout_weight="1"/> <Button android:id="@+id/button3" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/buttonbackground" android:drawableLeft="@drawable/mail" android:textColor="#ffffff" <!-- 删掉重复的android:margin="10px",保留layout_margin即可 --> android:layout_margin="10dp" android:paddingHorizontal="20dp" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Mail Us" android:layout_weight="1"/> </LinearLayout> <TextView android:id="@+id/textView" android:layout_margin="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Information" android:textAppearance="?android:attr/textAppearanceLarge" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@android:color/darker_gray"/> <TextView android:id="@+id/textView2" android:layout_margin="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/royal" android:textAppearance="?android:attr/textAppearanceSmall" /> </LinearLayout> </ScrollView>
额外补充几个小细节:
- 字体大小推荐用
sp单位,它会跟随系统的字体缩放设置,提升无障碍体验 - 你的Button里同时写了
android:margin和android:layout_margin,重复属性会导致冗余,直接保留layout_margin就行 android:elevation属性也建议用dp单位,保证阴影在不同设备上的视觉一致性
修改完这些之后,重新编译运行,应该就能解决这个错误啦!
内容的提问来源于stack exchange,提问作者BlueYeti81
相关产品推荐
相关产品推荐

