---
title: "Android开发中自定义View设定到FrameLayout布局中实现多组件显示"
author: "mmdev"
date: "2009-12-25T13:19:00Z"
tags: []
slug: "android-view-framelayout"
draft: false
lastmod: "2012-12-05T22:00:09Z"
id: 1742112
url: "https://mmdev.iteye.com/blog/1742112"
---

在Android开发中，如果我们想自定义View视图组件，并实现在FrameLayout布局中多个组件的同时显示呢？

举例来说想在自定义的View上面显示Button 等[View组件](http://www.3gdci.com)需要完成如下任务

1.在自定义View的类中覆盖父类的构造(注意是2个参数的)

public class MyView2 extends View{

**public MyView2(Context context,AttributeSet att)**  
{super(context,att);  
}  
**public void onDraw(Canvas c)**  
{//这里绘制你要的内容

}  
}

2.定义布局文件

&lt;?xml version="1.0" encoding="utf-8"?&gt;  
&lt;FrameLayout xmlns:android="[http://schemas.android.com/apk/res/android](http://writeblog.csdn.net/link.php?url=http%3A%2F%2Fschemas.android.com%2Fapk%2Fres%2Fandroid)"  
android:orientation="vertical"  
android:layout\_width="fill\_parent"  
android:layout\_height="fill\_parent"  
&gt;

&lt;com.lovose.MyView2  
android:id="@+id/View01"  
android:layout\_width="fill\_parent"  
android:layout\_height="fill\_parent"  
&gt;  
&lt;/com.lovose.MyView2&gt;

&lt;AbsoluteLayout android:id="@+id/AbsoluteLayout01" android:layout\_width="wrap\_content" android:layout\_height="wrap\_content"&gt;  
&lt;Button android:text="Button01" android:id="@+id/Button01" android:layout\_width="wrap\_content" android:layout\_height="wrap\_content" android:layout\_x="100dip" android:layout\_y="100dip"&gt;&lt;/Button&gt;

&lt;/AbsoluteLayout&gt;  
&lt;/FrameLayout&gt;

//哈哈,你可以任意定义UI的显示了