Android開發中自定義View設定到FrameLayout佈局中實現多組件顯示

在Android開發中,如果我們想自定義View視圖組件,並實現在FrameLayout佈局中多個組件的同時顯示呢?

舉例來說想在自定義的View上面顯示Button 等View組件需要完成如下任務

1.在自定義View的類中覆蓋父類的構造(注意是2個參數的)

1
2
3
4
5
6
7
8
9
10
public class MyView2 extends View{
 
  public MyView2(Context context,AttributeSet att)
 {super(context,att); 
 }
   public void onDraw(Canvas c)
    {  // 這裏繪製你要的內容
 
   }
}

2.定義佈局文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
 
<com.lovose.MyView2
 android:id="@+id/View01" 
  android:layout_width="fill_parent"
    android:layout_height="fill_parent"
 >
</com.lovose.MyView2>
 
<AbsoluteLayout android:id="@+id/AbsoluteLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content">
<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"></Button>
 
 
</AbsoluteLayout>
</FrameLayout>

//哈哈,你可以任意定義UI的顯示了

原文作者:sdhjob

原文鏈接:http://blog.csdn.net/sdhjob/archive/2009/12/25/5074718.aspx

發佈了7 篇原創文章 · 獲贊 4 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章