如何强制Android View缩小子级

时间:2011-01-06 03:58:46

标签: android android-layout relativelayout

我正在尝试为Android编写纸牌游戏,但我遇到了布局困难。我正在编写主游戏屏幕,其中包括五个垂直排列在左右两侧的卡片以及五个横向排列在顶部和底部的卡片。我遇到的问题是,如果所有其他卡片都没有空间显示,我需要水平和垂直缩小所有卡片。

下面是问题的图片(为了清楚起见,我省略了左侧和底部的卡片)。如您所见,右侧仅显示四张卡片 - 底部卡片部分遮挡。视图已经空间不足,因此它会丢弃不适合的卡的其余部分。

以下是截图链接,因为我无法发布图片:image

在这种情况下我想要的是Android降低所有顶牌和右牌的高度,直到底牌完全可见。它似乎并不复杂,但我无法让它工作。我使用RelativeLayout创建了下面的图像,并将所有顶部卡片彼此相邻,并将所有正确的卡片放在彼此之下(顶部卡片位于顶部卡片的右下方)。我尝试将所有卡添加到他们自己的View中,但这也不起作用。

有什么建议吗?谢谢!


好的,我把它靠近了(见底部的图片链接),但现在我遇到了相反的问题 - 我需要增加它。我相信这是因为我缩小了我用于西装的图像。正如你所看到的,它们都缩小到了一个小尺寸。我希望能够让每个按钮设置的LinearLayout水平/垂直展开以占用所有可用空间,而无需从LinearLayout的可见部分绘制最后一个按钮。理想情况下,我可以放入任何尺寸的图片,也可以适当调整大小,但我担心我会在布局引擎中要求一些循环逻辑。

这是我的XML文件。 HandView类可以容纳每组中的五张卡片。 5张卡之间有5个像素的边距。

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:shrinkColumns="*"
    android:background="#FF00FF00">

    <TableRow
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:background="#FF0000FF">
        <com.herscher.euchre.ui.HandView
          android:background="#FFFF0000"
            android:layout_column="1"
            android:orientation="horizontal"
            android:id="@+id/northHandView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal" />
    </TableRow>

    <TableRow
        android:layout_height="wrap_content"
        android:layout_width="wrap_content">
        <com.herscher.euchre.ui.HandView
            android:layout_column="0"
            android:orientation="vertical"
            android:id="@+id/westHandView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <com.herscher.euchre.ui.PlayArea
            android:layout_column="1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/playArea" />

        <com.herscher.euchre.ui.HandView
            android:layout_column="2"
            android:orientation="vertical"
            android:id="@+id/eastHandView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </TableRow>

    <TableRow
        android:layout_height="wrap_content"
        android:layout_width="wrap_content">
        <com.herscher.euchre.ui.HandView
            android:layout_column="1"
            android:orientation="horizontal"
            android:id="@+id/southHandView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </TableRow>

</TableLayout>

New image is here

有人有任何建议吗?

1 个答案:

答案 0 :(得分:0)

您可能希望查看使用LinearLayout的权重功能,这会强制子视图调整到可用空间的大小。在你的情况下,你可能希望它们都具有相同的重量。

相关问题