我有一个扩展View的类Bubble。我需要在我的片段中使用它,布局文件如下所示(请注意我的泡泡视图在此布局中显示):
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="#ffcc33">
<!-- ***** note that since I am here loading my Bubble class that extends View, that class
c-tor must take Context AND AttributeSet, otherwise loading this will crash.
See Bubble c-tor below *****
-->
<study.android.bubble.Bubble
android:id="@+id/bubbleAnimationView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff1122"
android:padding="20dp"/>
<!-- since parent is FrameLayout, this view will be stacked up in z-order -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="right|bottom"
android:layout_margin="10dp">
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
android:background="@null"/>
</LinearLayout>
</FrameLayout>
我的自定义视图Bubble的构造函数如下所示:
public class Bubble extends View {
public Bubble(Context context, AttributeSet attributesSet) {
super(context, attributesSet);
...
...
}
// drawing methods
...
...
}
当我想从我的片段中实例化它时,比如在onAttach()中,如何将它传递给AttributeSet?
谢谢,