Android Studio 上需要包含列标题和不同列宽的列表

发布于 2025-01-12 02:53:00 字数 356 浏览 0 评论 0原文

像这样:

Code     Product Description                 Stock    Tel  
P1234    Portland Cement 25kg                70       0208 1234567  
P4321    MGM Sharp Sand Bulk Bag 800kg       3        0799 98765433  

可变长度的产品描述最多可达 45 个字符。

为此,将在移动设备上使用更宽的水平显示。

我查了一下,发现gridview不支持不同宽度的列。 据我了解,列表视图可能是相同的。

有没有办法不使用 x,y 来定位列?

Like this:

Code     Product Description                 Stock    Tel  
P1234    Portland Cement 25kg                70       0208 1234567  
P4321    MGM Sharp Sand Bulk Bag 800kg       3        0799 98765433  

The variable-length product description may be up to 45 characters.

Will use the wider horizontal display on the mobile for this.

I checked and found gridview does not support different-width columns.
From what I understand it may be the same for listview.

Is there a way without using x,y for positioning the columns?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

少女七分熟 2025-01-19 02:53:00

您可以使用 GridLayout 或 (可能)与 TableLayout

还有 GridView,但据我所知,GridView 中的每个单元格都必须具有相同的大小。我不知道一个单元格是否可以跨越 GridView 中的多个行/列,但即使可以,并且由于每个单元格的大小必须相同,那么您必须计算有多少个单元格每个条目都需要您的数据。所以我建议使用前两个选项。

最后,RecyclerView可以在网格中排列View(通过GridLayoutManager),但我不知道它是否可以满足您的要求(例如作为每个单元格的可变大小),所以我将其留在这里,以防您想将其视为另一个可能的选项。

下面是一个使用 GridLayout 实现的示例:

import android.content.Context;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.GridLayout;
import android.widget.HorizontalScrollView;
import android.widget.ScrollView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private static void addTextViews(final ViewGroup viewGroup,
                                     final String... text) {
        final Context ctx = viewGroup.getContext();
        for (final String t: text) {
            final TextView view = new TextView(ctx);
            view.setText(t);
            viewGroup.addView(view);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final GridLayout viewGroup = new GridLayout(this);

        //Configure grid:
        viewGroup.setColumnCount(4); //Wrap every 4 columns.
        viewGroup.setUseDefaultMargins(true); //Insert default space between cells.
        viewGroup.setAlignmentMode(GridLayout.ALIGN_BOUNDS); //Required by 'setUseDefaultMargins(true)' documentation.

        //Populate the grid:
        addTextViews(viewGroup, "Code", "Product Description", "Stock", "Tel");
        addTextViews(viewGroup, "P1234", "Portland Cement 25kg", "70", "0208 1234567");
        addTextViews(viewGroup, "P4321", "MGM Sharp Sand Bulk Bag 800kg", "3", "0799 98765433");
        for (int i = 0; i < 40; ++i)
            addTextViews(viewGroup, Integer.toString(i), "MGM Sharp Sand Bulk Bag 800kg......", Integer.toOctalString(5 * i), "0799 98765433");

        //Add the grid to scroll views:
        final HorizontalScrollView horizontalScrollView = new HorizontalScrollView(this);
        horizontalScrollView.addView(viewGroup);
        final ScrollView verticalScrollView = new ScrollView(this);
        verticalScrollView.addView(horizontalScrollView);

        setContentView(verticalScrollView);
    }
}

TextView 被添加到网格中,从左上角单元格开始向右直到 4 code> 列已填充,在这种情况下,下一个 TextView 将按照相同的模式进入后续行。此行为是由于水平方向(这是 GridLayout 的默认值)以及将列数设置为 4 的组合所致。默认情况下,每个单元格跨越一行和一列。

GridLayout 没有内置的滚动支持,但您可以将其添加到 Horizo​​ntalScrollView 和垂直 ScrollView 中,如上面的示例代码,为了启用滚动。

您始终可以在这些 ViewGroup 的文档中查看有关这些 ViewGroup 的更多信息。

You can try this with a GridLayout or (possibly) with a TableLayout.

There is also GridView, but as far as I know every cell in a GridView must have the same size with every other. I don't know if a cell could span multiple rows/columns in a GridView though, but even if it can and since every cell must be the same size, then you would have to calculate how many cells your data would require for each entry. So I would suggest going with the previous 2 options.

Finally, RecyclerViews can arrange Views in a grid (via GridLayoutManager), but I am not aware if it can fit your requirements (such as variable size for each cell), so I am just leaving it here in case you want to look at it as another possible option.

Here is an example implementation with a GridLayout:

import android.content.Context;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.GridLayout;
import android.widget.HorizontalScrollView;
import android.widget.ScrollView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private static void addTextViews(final ViewGroup viewGroup,
                                     final String... text) {
        final Context ctx = viewGroup.getContext();
        for (final String t: text) {
            final TextView view = new TextView(ctx);
            view.setText(t);
            viewGroup.addView(view);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final GridLayout viewGroup = new GridLayout(this);

        //Configure grid:
        viewGroup.setColumnCount(4); //Wrap every 4 columns.
        viewGroup.setUseDefaultMargins(true); //Insert default space between cells.
        viewGroup.setAlignmentMode(GridLayout.ALIGN_BOUNDS); //Required by 'setUseDefaultMargins(true)' documentation.

        //Populate the grid:
        addTextViews(viewGroup, "Code", "Product Description", "Stock", "Tel");
        addTextViews(viewGroup, "P1234", "Portland Cement 25kg", "70", "0208 1234567");
        addTextViews(viewGroup, "P4321", "MGM Sharp Sand Bulk Bag 800kg", "3", "0799 98765433");
        for (int i = 0; i < 40; ++i)
            addTextViews(viewGroup, Integer.toString(i), "MGM Sharp Sand Bulk Bag 800kg......", Integer.toOctalString(5 * i), "0799 98765433");

        //Add the grid to scroll views:
        final HorizontalScrollView horizontalScrollView = new HorizontalScrollView(this);
        horizontalScrollView.addView(viewGroup);
        final ScrollView verticalScrollView = new ScrollView(this);
        verticalScrollView.addView(horizontalScrollView);

        setContentView(verticalScrollView);
    }
}

The TextViews are added to the grid starting from the top-left cell and going to the right until 4 columns are filled, in which case the next TextViews will go in subsequent rows following the same pattern. This behaviour is because of the combination of horizontal orientation (which is the default value for the GridLayout), together with setting the column count to 4. By default, each cell spans a single row and a single column.

The GridLayout does not have built in support for scrolling, but you can add it to both a HorizontalScrollView and a vertical ScrollView, as demonstrated in the above sample code, in order to enable scrolling.

You can always look more information about those ViewGroups in their documentation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文