我想在屏幕顶部并排创建 5 个大小相同的外部正方形。每个外方格应包含 3 个大小相同的小方格,并排放置在每个外方格的顶部。3 个小方块中的每一个都是 TextView。
外部方块和内部方块都需要一个边框,我想用它来区分每个方块,这样用户就可以清楚地看到有 5 个不同的外部方块,每个外部方块中有 3 个不同的内部方块。
通过声明 5 个单独的 TableLayout,指定 layout_width="0dp" 和 layout_weight="2",我能够获得 5 个大小相同的外部正方形。在每个 TableLayout 中,我定义了一个带有 3 个 TextView 的元素。
如何使包含 TextViews 的 3 个内部正方形中的每一个都具有相同的大小,而与屏幕大小无关?我尝试将每个 TextViews 的 layout_weight 设置为等于“.3”,但这并不能解决问题。
我能够使用 TextViews 获得 3 个相同大小的正方形的唯一方法是为 3 个迷你正方形中的每一个设置 android:minWidth="12dp" (或除 12dp 之外的其他数字),但显然这不适用于不同的大小的屏幕。
我的表的每个设置都类似于下面的 TableLayout。我包含的这个布局是允许我正确创建迷你方块的布局,但不适用于不同尺寸的屏幕。我使用边距实际创建了 TextViews 周围的边框(带有白色轮廓的黑色背景)。
除了这个布局,我尝试创建一个 Shape 资源并将 3 个小方块的背景设置为该 Shape,但这也不起作用,但我也包含了该资源。
<TableLayout
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#FFFFFF">
<TableRow android:background="#FFFFFF">
<TextView
android:background="#000000"
android:layout_marginLeft="1dip"
android:layout_marginRight="1dip"
android:layout_marginBottom="1dip"
android:minWidth="12dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:background="#000000"
android:layout_marginLeft="1dip"
android:layout_marginRight="1dip"
android:layout_marginBottom="1dip"
android:minWidth="12dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:background="#000000"
android:layout_marginLeft="1dip"
android:layout_marginBottom="1dip"
android:layout_marginRight="1dip"
android:minWidth="12dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</TableRow>
</TableLayout>
我尝试的 Shape 资源是:
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#000000"/>
<stroke android:width="1dp" color="#000000"/>
<corners android:radius="1dp"/>
<padding android:left="2dp" android:top="1dp"
android:right="2dp" android:bottom="1dp" />
</shape>