试图使表行在表布局中的高度相等

问题描述 投票:0回答:1

我正在使用TableLayout,其中每个表行将由三个单元格组成,每个单元格将显示一个图像。我希望每个imageview /单元格具有相同的高度和宽度。问题是,默认情况下,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>

这里是目前的样子enter image description hereenter image description here

java android xml android-tablelayout
1个回答
0
投票

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

一个:

在您的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)的布局。

两个:将相同尺寸的图像发送到您的应用或将其调整为所需的尺寸

我更喜欢两种方式。

在评论中更新我

© www.soinside.com 2019 - 2024. All rights reserved.