2012-10-14 88 views
0

我有一個由一些按鈕組成的TableLayout。我想在TableLayout周圍繪製一個矩形。我不知道如何做到這一點。從我的研究中,我發現我需要擴展視圖並使用此視圖的onDraw事件。但是,我很困惑的一點是「如何在視圖中包含TableLayout」?桌布佈局的矩形

在此先感謝您的答案。

我的示例代碼

<?xml version="1.0" encoding="utf-8"?> 

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/numkeypad" 
     android:orientation="vertical" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:stretchColumns="*" 
     android:layout_gravity="bottom" 
     android:visibility="visible" 
     android:background="@color/contents_text" 
    > 
<TableRow> 
    <LinearLayout android:layout_height="wrap_content" 
        android:layout_width="0dp" 
        android:background="#404040" 
        android:layout_span="2"> 
     <Button android:id="@+id/Test1" 
       android:layout_height="wrap_content" 
       android:layout_width="wrap_content" 
       android:layout_weight="1" 
       android:textColor="#000000" 
       android:text="Test1" 
       android:textSize="20dp" 
       android:textStyle="bold" 
       > 
     </Button> 
     <Button android:id="@+id/Test2" 
       android:layout_height="wrap_content" 
       android:layout_width="wrap_content" 
       android:layout_weight="1" 
       android:textColor="#000000" 
       android:text="Test2" 
       android:textSize="20dp" 
       android:textStyle="bold" 
       > 
     </Button> 
    </LinearLayout> 
</TableRow> 
<TableRow> 
    <LinearLayout android:layout_height="wrap_content" 
        android:layout_width="0dp" 
        android:background="#404040" 
        android:layout_span="2"> 
     <Button android:id="@+id/Test11" 
       android:layout_height="wrap_content" 
       android:layout_width="wrap_content" 
       android:layout_weight="1" 
       android:textColor="#000000" 
       android:text="Test11" 
       android:textSize="20dp" 
       android:textStyle="bold" 
       > 
     </Button> 
     <Button android:id="@+id/Test22" 
       android:layout_height="wrap_content" 
       android:layout_width="wrap_content" 
       android:layout_weight="1" 
       android:textColor="#000000" 
       android:text="Test22" 
       android:textSize="20dp" 
       android:textStyle="bold" 
       > 
     </Button> 
    </LinearLayout> 
</TableRow> 

</TableLayout> 

和輸出我是

enter image description here

而且我要的是像

enter image description here

回答

0

可以擴展的FrameLayout畫t他矩形根據它在的onDraw()方法尺寸 說你擴展的類是com.example.MyFrameLayout(主叫 .onDraw()當然後)。

然後你可以聲明如下佈局:

<?xml version="1.0" encoding="utf-8"?> 
<com.example.MyFrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 

    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/numkeypad" 
     android:orientation="vertical" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:stretchColumns="*" 
     android:layout_gravity="bottom" 
     android:visibility="visible" 
     android:background="@color/contents_text"> 

     ... 
    </TableLayout> 
</com.example.MyFrameLayout> 
+0

爲什麼我得到「沒有默認的構造函數中android.widget.FrameLayout可用」當我試圖在我的課擴展的FrameLayout? – PCoder

+0

在佈局xml中使用自定義視圖時,需要定義以下構造函數(對於我的示例):MyFrameLayout(上下文上下文,AttributeSet attrs) - 只需調用super(context,attrs),並在需要時進一步初始化。 – Genry

+0

嗨,感謝您對構造函數的幫助。我現在擁有像你說的一切,但我從來沒有對新佈局的構造函數進行過調用。這可能是因爲this.setWillNotDraw(false)從構造函數中丟失了。添加後,我有一個完美的形狀,我正在尋找。 – PCoder