【问题标题】:insert an image in a tablelayout without stretching it Android在表格布局中插入图像而不拉伸它Android
【发布时间】:2011-02-12 09:24:21
【问题描述】:
我在表格布局中获取图片时遇到了一些问题...实际上,每列都有 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>
【问题讨论】:
标签:
android
image
tablelayout
【解决方案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>
以上面的代码为例。
【解决方案2】:
另一种解决方案是将 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>