Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

在自訂 View 上疊加元件:FrameLayout 混合排版實作

編輯:Android 開發實例

若想在自訂的 View 上顯示 Button 等控制項,可以善用 FrameLayout 的層疊特性。以下分兩步完成。

步驟一:撰寫自訂 View 類別

注意這裡要覆寫的是「兩個參數」的建構子:

public class MyView2 extends View{
  public MyView2(Context context,AttributeSet att) { super(context,att); }
  public void onDraw(Canvas c) { // 在此繪製你要的內容 }
}

步驟二:定義版面配置

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="https://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 的顯示了

FrameLayout 會讓子元件彼此重疊堆疊,因此自訂 View 畫在底層,Button 等控制項疊在上層,就能實現自繪圖形與互動元件共存的複合介面。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved