Android 片段中自定义视图的属性集
我有一个扩展视图的类气泡。我需要在我的片段中使用它,布局文件如下所示(请注意,我的气泡视图显示在此布局中): 当我想从片段中实例化它时,比如在onAttach()中,如何传递它AttributeSetAndroid 片段中自定义视图的属性集,android,view,fragment,device-orientation,Android,View,Fragment,Device Orientation,我有一个扩展视图的类气泡。我需要在我的片段中使用它,布局文件如下所示(请注意,我的气泡视图显示在此布局中): 当我想从片段中实例化它时,比如在onAttach()中,如何传递它AttributeSet 谢谢,你真的需要通过属性设置吗?我的意思是,您在自定义视图类中使用它?您在布局文件中看到我的注释了吗? <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.androi
谢谢,你真的需要通过属性设置吗?我的意思是,您在自定义视图类中使用它?您在布局文件中看到我的注释了吗?
<?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>
public class Bubble extends View {
public Bubble(Context context, AttributeSet attributesSet) {
super(context, attributesSet);
...
...
}
// drawing methods
...
...
}