2017-04-21 93 views
1

有没有办法将默认的android attrs设置为自定义视图,当在xml中定义此视图时可以使用该方法?例如,我需要创建CustomImageView,其中FrameLayoutImageViewProgressBar位于其中。如果我在xml中添加这样的视图,例如,我想设置该视图的背景或src。 FrameLayout没有android:src属性,它不通过Android Studio自动完成窗口提供,我可以让Android Studio知道此布局可以处理这些属性吗?android:src attrs自定义视图

回答

1

诀窍是把android:src定制styleables,位于values/attrs.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <declare-styleable name="MyView"> 
     <attr name="android:src"/> 
    </declare-styleable> 
</resources> 

,并具有MyView类,它是一个View的后代,然后在xml文件中,我们会看到这一点:

enter image description here