2012-10-14 89 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