【问题标题】:Trying to get table rows equal height in table layout试图在表格布局中使表格行高度相等
【发布时间】:2020-03-19 17:55:41
【问题描述】:

我正在使用 TableLayout,其中每个表格行将包含三个单元格,每个单元格将显示一个图像。我希望每个图像视图/单元格的高度和宽度相等。问题是,默认情况下,tablerow 的高度会包裹到具有最大高度的 imageview/cell 的大小,因此行的高度不相等。

        TableLayout tableLayout = (TableLayout) getActivity().findViewById(R.id.tableLayout);
            TableRow.LayoutParams lp = new TableRow.LayoutParams(tableLayout.getWidth()/3, ViewGroup.LayoutParams.WRAP_CONTENT, 0.3f);
            TableRow tableRow = new TableRow(getActivity().getApplicationContext());
            lp.setMargins(1,1,1,1);
            tableRow.setLayoutParams(lp);
            imageView.setImageBitmap(b);
            imageView.setLayoutParams(lp);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            tableRow.addView(imageView);
            tableLayout.addView(tableRow);

这里是xml

            <TableLayout
                android:id="@+id/tableLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:stretchColumns="*"
                android:weightSum="1"
                app:layout_constrainedHeight="false"
                app:layout_constraintHeight_percent="0.5"
                app:layout_constraintWidth_percent="1">
            </TableLayout>

这是目前的样子

【问题讨论】:

    标签: java android xml android-tablelayout


    【解决方案1】:

    你可以使用两种方法来解决你的问题

    一个:

    在你的 xml 中设置类似这样的代码

            <TableLayout
                android:id="@+id/tableLayout"
                android:layout_width="200dp"
                android:layout_height="200dp"
                app:layout_constrainedHeight="false"
                app:layout_constraintHeight_percent="0.5"
                app:layout_constraintWidth_percent="1">
            </TableLayout>
    

    通过这种方式,您可以像 Instagram 一样使用您想要的宽度和高度。

    两个: 向您的应用发送相同大小的图像或将其调整为您想要的大小

    我更喜欢两种方式一起使用。

    在 cmets 中更新我

    【讨论】:

    • 我不喜欢像这样对宽度和高度进行硬编码。我更喜欢它是动态的,因此它可以适应各种屏幕尺寸和纵横比,我想我可以尝试调整它的大小。
    • resize 可能会导致您的图像变丑而不好,请尝试并仔细检查
    • 最终将 TableLayout 参数高度硬编码为 300dp,似乎可以在手机设备上很好地工作。
    • 很高兴听到这个消息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-01
    • 1970-01-01
    • 2017-11-13
    • 2015-02-03
    • 2013-05-16
    相关资源
    最近更新 更多