【发布时间】:2015-11-09 18:42:03
【问题描述】:
我在 TableRow 中有一个 ImageView,如下面的 xml 所示。我的问题是,当行高发生变化时,图像会按比例放大。我该如何避免这种情况?
我尝试以编程方式设置 LinearLayout.LayoutParams 无济于事。也尝试设置 ImageView 的 maxWidth。
我还应该提到我已经使用下面给出的类(使用 onLayout)修复了表格的标题。
<?xml version="1.0" encoding="utf-8"?>
<TableRow
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/trTransationTableRow"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.ui.components.CustomTextView
android:id="@+id/tvTRCol1"
style="@style/transaction_table_row"
android:layout_width="0dp"
android:gravity="left|center_vertical"
android:layout_weight="0.2"/>
<com.ui.components.CustomTextView
android:id="@+id/tvTRCol2"
style="@style/transaction_table_row"
android:layout_width="0dp"
android:singleLine="false"
android:layout_weight="0.4"/>
<ImageView
android:id="@+id/ivTRCol3"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:layout_weight="0.2"
android:maxWidth="10dp"
android:adjustViewBounds="true"
android:background="@drawable/table_row_border"/>
<com.ui.components.CustomTextView
android:id="@+id/tvTRCol4"
style="@style/transaction_table_row"
android:layout_width="0dp"
android:gravity="right|center_vertical"
android:layout_weight="0.2"/>
</TableRow>
可滚动表格布局类:
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
List<Integer> colWidths = new LinkedList<Integer>();
TableLayout header = (TableLayout) findViewById(R.id.tlScrollingTableHeader);
TableLayout body = (TableLayout) findViewById(R.id.tlScrollingTableBody);
// Measure content width first
for (int rownum = 0; rownum < body.getChildCount(); rownum++) {
TableRow row = (TableRow) body.getChildAt(rownum);
int countCells = 0;
for (int cellnum = 0; cellnum < row.getChildCount(); cellnum++) {
View cell = row.getChildAt(cellnum);
if (cell.getVisibility() == VISIBLE) {
Integer cellWidth = cell.getWidth();
if (colWidths.size() <= countCells) {
colWidths.add(cellWidth);
} else {
Integer current = colWidths.get(countCells);
if (cellWidth > current) {
colWidths.remove(countCells);
colWidths.add(countCells, cellWidth);
}
}
countCells++;
}
}
}
// Figure out if header needs resizing first based on widths
TableRow headerRow = (TableRow) header.getChildAt(0);
for (int count = 0; count < colWidths.size(); count++) {
if (headerRow.getChildAt(count).getWidth() >= colWidths.get(count)) {
colWidths.remove(count);
colWidths.add(count, headerRow.getChildAt(count).getWidth());
}
}
// Then apply to header
if (colWidths.size() == 4) {
for (int cellnum = 0; cellnum < headerRow.getChildCount(); cellnum++) {
View cell = headerRow.getChildAt(cellnum);
TableRow.LayoutParams params = (TableRow.LayoutParams) cell.getLayoutParams();
params.width = colWidths.get(cellnum);
}
}
}
【问题讨论】:
-
你不能使用imageview的src吗?或者只使用9个补丁图像。
-
我正在动态设置 ImageView src。如您所见,它可以是不同的图像。
-
但是您在哪里将 src 图像设置为 imageview?
-
“当行高改变时,图像会放大” - 我不是 100% 肯定,但我认为它必须这样做,因为你设置了 android:height="match_parent"。你在那里设置了最大宽度,你不能对高度做同样的事情吗?
-
或者在你的 xml 的图像视图中查看这个。
android:background="@drawable/table_row_border"android:layout_height="match_parent"如果你使用权重而不是像等待其他视图一样通过定义宽度 0 和等待 .2 来使用它
标签: android android-imageview android-tablelayout tablerow