0

我在表格布局中获取图片时遇到了一些麻烦...实际上,每个我都有 2 列,当我将图片放在表格的一个单元格中时,它完全被拉伸和不成比例...我找不到如何制作它保持在单元格内的原始状态,并让其余的空间由白色背景填充...... XML 是这样的:

<TableLayout android:layout_width="fill_parent" android:stretchColumns="*"
android:layout_marginLeft="3dip" android:layout_marginRight="10dip"
android:layout_height="wrap_content">

<TableRow android:layout_marginBottom="10dip">
<TextView android:id="@+id/tv01" android:text="text1"
 android:textSize="14sp" android:layout_margin="1dip"
 android:layout_height="wrap_content" android:textColor="@color/bleuLink"
 android:layout_width="wrap_content" android:layout_weight="1"
 android:background="@color/background"></TextView>

 <Button android:id="@+id/add" android:background="@drawable/addressbook"
  android:layout_height="wrap_content" android:layout_width="wrap_content"></Button>
 <Button android:id="@+id/call" android:background="@drawable/greenphone"
  android:layout_height="wrap_content" android:layout_width="wrap_content"></Button>

4

2 回答 2

1

由于这行代码,您的图像正在拉伸。

<TableLayout android:layout_width="fill_parent" android:stretchColumns="*" />

因为您告诉您的表格填充其父级,然后将可拉伸列设置为全部,所以您的布局会强制其子级填充所需的空间。删除可拉伸列属性或将其设置为要拉伸的列的以逗号分隔的索引列表。

   <TableLayout android:layout_width="fill_parent"
        android:stretchColumns="0, 2" android:layout_marginLeft="3dip"
        android:layout_marginRight="10dip" android:layout_height="wrap_content">
        <TableRow android:layout_marginBottom="10dip">
            <TextView android:id="@+id/tv01" android:text="text1"
                android:textSize="14sp" android:layout_margin="1dip"
                android:layout_height="wrap_content" 
                android:layout_width="wrap_content" android:layout_weight="1"></TextView>

            <Button android:id="@+id/add" android:background="@drawable/icon"
                android:layout_height="wrap_content" android:layout_width="wrap_content"></Button>
            <Button android:id="@+id/call" android:background="@drawable/icon"
                android:layout_height="wrap_content" android:layout_width="wrap_content"></Button>
        </TableRow>
    </TableLayout>

试试上面的代码作为例子。

于 2010-04-26T21:47:51.177 回答
0

另一种解决方案是将 Button 添加到布局中并设置布局属性 android:layout_height="wrap_content"。

<TableLayout android:layout_width="fill_parent"
    android:stretchColumns="*" android:layout_height="wrap_content">
    <TableRow>
        <LinearLayout android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:gravity="center">
            <TextView android:id="@+id/tv01" android:text="text1"
                android:textSize="14sp" android:layout_margin="1dip"
                android:layout_height="wrap_content" 
                android:layout_width="wrap_content" android:layout_weight="1">
            </TextView>
        </LinearLayout>

        <LinearLayout android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:gravity="center">
            <Button android:id="@+id/add" android:background="@drawable/icon"
                android:layout_height="wrap_content" android:layout_width="wrap_content"> 
            </Button>
        </LinearLayout>

        <LinearLayout android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:gravity="center">
            <Button android:id="@+id/call" android:background="@drawable/icon"
                android:layout_height="wrap_content" android:layout_width="wrap_content">
            </Button>
        </LinearLayout>
    </TableRow>
</TableLayout>
于 2010-08-30T03:28:48.093 回答