【问题标题】:How to force Android View to shrink children如何强制Android View缩小孩子
【发布时间】:2011-01-06 03:58:46
【问题描述】:

我正在尝试为 Android 编写纸牌游戏,但遇到了布局困难。我正在编写主游戏屏幕,它由左右垂直排列的五张卡片和上下水平排列的五张卡片组成。我遇到的问题是,如果没有空间显示所有其他卡片,我需要卡片在水平和垂直方向上全部收缩。

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

这是屏幕截图的链接,因为我无法发布图片:image

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

有什么建议吗?谢谢!


好的,我已经接近了(请参阅底部的图片链接)但现在我遇到了相反的问题 - 我需要增加它。我相信这是因为我缩小了用于西装的图像。如您所见,它们都缩小到很小的尺寸。我希望能够为每个按钮设置 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

有人有什么建议吗?

【问题讨论】:

    标签: android android-layout android-relativelayout


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-06
      • 1970-01-01
      • 2011-12-09
      • 1970-01-01
      • 2011-10-07
      • 1970-01-01
      • 2014-04-14
      • 1970-01-01
      相关资源
      最近更新 更多