问题描述
我试图弄清楚如何通过xml通过android:gravity
获得自定义视图.
I'm trying to figure out how to pass android:gravity
via xml for a custom view.
此处发布的解决方案( https://stackoverflow.com/a/3441986/413254 )表示要添加android:gravity
归于其他attr的属性.
The solution posted here (https://stackoverflow.com/a/3441986/413254) says to add the android:gravity
attribute to the other attr's.
执行此操作时,我会收到警告:
When I do this, I end up with a warning saying:
错误:属性"android:gravity"已经定义 错误:任务':app:processStagingDebugResources'的执行失败.
Error:Attribute "android:gravity" has already been defined Error:Execution failed for task ':app:processStagingDebugResources'.
我的colors.xml文件?
My colors.xml file?
values.xml的第165行是
Line 165 of values.xml is
<declare-styleable name="StackedTextView">
<attr format="integer" name="android:gravity"/>
<attr format="string" name="line1"/>
<attr format="string" name="line2"/>
<attr format="dimension" name="line1_textSize"/>
<attr format="dimension" name="line2_textSize"/>
</declare-styleable>
在多个组中多次出现android:gravity.主题,FlowLayout(我包括的库),LinearLayoutCompat等.
There are multiple occurrences of android:gravity in multiple groups. Theme, FlowLayout (library I include), LinearLayoutCompat, etc.
更新了错误消息.
推荐答案
编写如下:
<attr format="integer" name="android:gravity"/>
您要引入具有整数格式的新属性android:gravity
,当然,系统中已经定义了android:gravity
,因此您需要将其更改为:
you are intruducing the new attribute android:gravity
with integer format, of course android:gravity
is already defined in the system, so you need to change it into:
<attr name="android:gravity"/>
这篇关于如何通过xml为自定义视图传递重力属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!