Android 表格布局周围的矩形

Android 表格布局周围的矩形,android,android-layout,android-tablelayout,Android,Android Layout,Android Tablelayout,我有一个由一些按钮组成的表格布局。我想在TableLayout周围画一个矩形。我不知道如何才能做到这一点。从我的研究中,我发现我需要扩展一个视图并使用该视图的onDraw事件。但是,我感到困惑的一点是“如何在视图中包含TableLayout” 提前感谢您的回答 我的示例代码是 <?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res

我有一个由一些按钮组成的表格布局。我想在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>

我得到的结果是

我想要的是


您可以在onDraw()方法中扩展FrameLayout以根据矩形的尺寸绘制矩形(当然,在调用super.onDraw()之后) 假设您的扩展类是com.example.MyFrameLayout

然后可以声明以下布局:

<?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>


当我尝试在类中扩展FrameLayout时,为什么会出现“android.widget.FrameLayout中没有默认构造函数可用”的情况?当您在layout xml中使用自定义视图时,需要定义以下构造函数(例如:MyFrameLayout(上下文上下文,AttributeSet attrs)-只需调用super(上下文,attrs)如果需要的话,可以进行进一步的初始化。您好,谢谢您对构造函数的帮助。我现在拥有了你所说的一切,但我从未调用过新布局上的构造函数。这可能是因为构造函数中缺少此.setWillNotDraw(false)。加上这个后,我有一个完美的形状,我一直在寻找。