避免 ImageView 在 TableRow 内缩放

Avoid ImageView from scaling inside a TableRow

我在 TableRow 中有一个 ImageView,如下面的 xml 所示。我的问题是,每当行高发生变化时,图像都会按比例放大。我该如何避免这种情况?

我曾尝试以编程方式设置 LinearLayout.LayoutParams 无济于事。还尝试设置 ImageView 的最大宽度。

我还应该提到我已经通过使用 class(使用 onLayout)修复了 table 的 header,如下所示。

<?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>

可滚动table布局class:

@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);
        }
    }
}

经过反复试验,我找到了如下解决方案。 scaleType="centerInside" 背后的魔法,但需要确保两者 layout_widthlayout_height 设置为 wrap_content。我也有 LinearLayout 来获得诸如后台工作之类的东西。希望这对某人有帮助

<LinearLayout
    android:id="@+id/llTRCol3"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="0.2"
    android:gravity="center"
    android:visibility="gone"
    android:background="@drawable/table_row_border">
    <ImageView
        android:id="@+id/ivTRCol3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:scaleType="centerInside"/>
</LinearLayout>