2017-04-21 95 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