Referencing custom view in XML doesn´t work both documented ways?

给你一囗甜甜゛ 提交于 2019-12-19 10:12:07

问题


<com.test.www.BrushPreview android:id="@+id/brushview"
    android:layout_width="100dip" android:layout_height="100dip"
    android:layout_gravity="center" />

and this not:

<View class="com.test.www.BrushPreview" android:id="@+id/brushview"
    android:layout_width="100dip" android:layout_height="100dip"
    android:layout_gravity="center" />

Following the documentation http://developer.android.com/guide/topics/ui/custom-components.html#modifying it should work both ways! What am I doing wrong?

edit: lowercase view seems to work! i thought i tried that already...thanks! and furthermore, it´s very important to use this constructor in the view class:

public BrushPreview(Context context, AttributeSet attrs) 

otherwise it won´t get called.


回答1:


You should use lowercase view in the xml for your custom layout:

<view class="com.test.www.BrushPreview" android:id="@+id/brushview"
    android:layout_width="100dip" android:layout_height="100dip"
    android:layout_gravity="center" />

(according to your link too), and it should work.

Otherwise it tries to create a android.view.View instance.



来源:https://stackoverflow.com/questions/5946347/referencing-custom-view-in-xml-doesn%c2%b4t-work-both-documented-ways

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!