AAPT错误:预期维度值却获match_parent问题求助
解决AAPT错误:expected dimension but got (raw string) match_parent
问题根源
你在样式里错误使用了android:width和android:height属性。这两个属性要求传入具体尺寸值(比如120dp、20sp),而match_parent/wrap_content是布局参数专属的取值,只能写在布局文件控件标签的android:layout_width/android:layout_height中,不能用在样式的android:width/android:height里。
修复步骤
- 先删除样式中的错误配置行:
<item name="android:width">match_parent</item> <item name="android:height">wrap_content</item>
- 若需要统一设置控件宽高,可选择以下两种方式:
- 方式一:布局文件直接设置
在使用该样式的控件标签里添加布局参数:<TextView style="@style/Theme.DiskPizzaCascavel.TxtInformationBase" android:layout_width="match_parent" android:layout_height="wrap_content" <!-- 其他属性 --> /> - 方式二:样式中定义布局参数
若希望样式包含布局规则,可修改样式为:
注意这种方式要求样式仅应用到支持对应布局参数的控件上,且布局容器(如ConstraintLayout、LinearLayout)能识别该LayoutParams。<style name="Theme.DiskPizzaCascavel.TxtInformationBase"> <item name="android:layout_width">match_parent</item> <item name="android:layout_height">wrap_content</item> <!-- 其他样式属性 --> </style>
- 方式一:布局文件直接设置
额外说明
你尝试修改SDK版本、切换Material库版本的操作对这个问题没有帮助,因为错误核心是样式属性的误用,和依赖版本无关。
内容的提问来源于stack exchange,提问作者Bnteo Costa
相关产品推荐
相关产品推荐

