Referencing custom view in XML doesn´t work both d

2019-02-19 18:36发布

问题:

<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.