避免ImageView在TableRow中缩放

时间:2015-11-09 07:18:32

标签: android android-imageview android-tablelayout tablerow

我在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);
        }
    }
}

enter image description here

1 个答案:

答案 0 :(得分:1)

经过一些试验和错误后,我找到了解决方案如下。魔术落后于 scaleType =&#34; centerInside&#34; ,但需要确保 layout_width layout_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>
相关问题