简体   繁体   English

如何在运行时使用自定义(水平和垂直)分隔符创建动态TableLayout

[英]How to create dynamic TableLayout in runtime with custom (horizontal and vertical) dividers

Basically I want to create function which dynamically create a TableLayout with horizontal and vertical custom dividers which I insert into FrameLayout (container). 基本上,我想创建一个函数,该函数动态创建带有水平和垂直自定义分隔符的TableLayout,并将其插入FrameLayout(容器)中。 I can easily do that in XML, but I need to do that in runtime, mainly because I dont know how big the table gonna be, also it might be big (want to avoid copy+paste code in XML many times). 我可以在XML中轻松地做到这一点,但是我需要在运行时中做到这一点,主要是因为我不知道该表将有多大,它也可能很大(想避免多次使用XML复制和粘贴代码)。

Here is what I want exactly (tho static), but in XML: 这正是我想要的(静态),但是使用XML:

<TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/unitsTableContainer"
        android:layout_margin="30dp">

        <View style="@style/TableVerticalDivider"/>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <View style="@style/TableHorizontalDivider"/>

            <TextView style="@style/UnitTableDefaultText"/>

            <View style="@style/TableHorizontalDivider"/>

            <TextView style="@style/UnitTableDefaultText"/>

            <View style="@style/TableHorizontalDivider"/>

            <TextView style="@style/UnitTableDefaultText"/>

            <View style="@style/TableHorizontalDivider"/>

            <TextView style="@style/UnitTableDefaultText"/>

            <View style="@style/TableHorizontalDivider"/>

        </TableRow>

        <View style="@style/TableVerticalDivider"/>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <View style="@style/TableHorizontalDivider"/>

            <TextView style="@style/UnitTableDefaultText"/>

            <View style="@style/TableHorizontalDivider"/>

            <TextView style="@style/UnitTableDefaultText"/>

            <View style="@style/TableHorizontalDivider"/>

            <TextView style="@style/UnitTableDefaultText"/>

            <View style="@style/TableHorizontalDivider"/>

            <TextView style="@style/UnitTableDefaultText"/>

            <View style="@style/TableHorizontalDivider"/>

        </TableRow>

        <View style="@style/TableVerticalDivider"/>

    </TableLayout>

Here is my best result doing it in runtime (dynamic): 这是我在运行时(动态)执行此操作的最佳结果:

private void createTable() {
    TableLayout tableLayout;
    View verticalTableColumnDivider;
    View horizontalTableRowDivider;
    TableRow tableRow;
    TextView textView;

    tableLayout = new TableLayout(getApplicationContext());
    tableLayout.setStretchAllColumns(true);

    for (int currentRow = 0; currentRow < 2; currentRow++) {
        tableRow = new TableRow(getApplicationContext());

        verticalTableColumnDivider = getLayoutInflater().inflate(R.layout.template_vertical_table_divider, null);
        horizontalTableRowDivider = getLayoutInflater().inflate(R.layout.template_horizontal_table_divider, null);
        tableRow.addView(horizontalTableRowDivider);

        for (int currentColumn = 0; currentColumn < 4; currentColumn++) {
            textView = (TextView) getLayoutInflater().inflate(R.layout.template_default_table_text, null);
            tableRow.addView(textView);

            horizontalTableRowDivider = getLayoutInflater().inflate(R.layout.template_horizontal_table_divider, null);
            tableRow.addView(horizontalTableRowDivider);
        }
        tableLayout.addView(verticalTableColumnDivider);
        tableLayout.addView(tableRow);
    }

    verticalTableColumnDivider = getLayoutInflater().inflate(R.layout.template_vertical_table_divider, null);
    tableLayout.addView(verticalTableColumnDivider);

    FrameLayout tableLayoutContainer = (FrameLayout) findViewById(R.id.unitsTableContainer);
    tableLayoutContainer.addView(tableLayout);
}

Here are templates and their styles (ofcourse styles are in one XML file, and all 3 templates are in separated layout XML file): 以下是模板及其样式(当然,样式在一个XML文件中,而所有3个模板在单独的布局XML文件中):

<View style="@style/TableHorizontalDivider"/>

<style name="TableHorizontalDivider">
    <item name="android:layout_width">4dp</item>
    <item name="android:layout_height">match_parent</item>
    <item name="android:background">?android:attr/listDivider</item>
    <item name="android:visibility">visible</item>
</style>

<View style="@style/TableVerticalDivider"/>

<style name="TableVerticalDivider">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">4dp</item>
    <item name="android:background">?android:attr/listDivider</item>
    <item name="android:visibility">visible</item>
</style>

<TextView style="@style/UnitTableDefaultText"/>

<style name="UnitTableDefaultText">
    <item name="android:textColor">#222222</item>
    <item name="android:text">Test</item>
    <item name="android:textSize">16sp</item>
    <item name="android:gravity">center</item>
    <item name="android:layout_gravity">center</item>
    <item name="android:layout_weight">1</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_width">0dp</item>
</style>

And here is result, top table is made dynamically, bottom table is what it should look like but made statically: result of my tries 结果是,顶层表格是动态生成的,底层表格是它应该看起来像但静态生成的: 我的尝试结果

No problems with putting textViews into TableLayout, as you can see only dividers are problem, row dividers are too thin, and column dividers (if you look closely) they are also too thin and stacked on row dividers, dont know why they are drawn horizontally instead of vertically. 将textViews放到TableLayout中没有问题,因为您只能看到分隔符是有问题的,行分隔符太细了,而列分隔符(如果您仔细看的话)也太细了,并且堆积在行分隔符上,不知道为什么它们是水平绘制的而不是垂直。 So if anybody knows why dividers are drawn wrong, and / or have solution how to do that differently, please let me know. 因此,如果有人知道分隔线绘制错误的原因,和/或有解决方法,请以不同的方式进行操作,请告诉我。

OK, I found out how to do this. 好的,我知道了该怎么做。 I made a class where you put your text content as String[][] for the table, and it creates the table in runtime: 我创建了一个类,您在其中将文本内容作为表的String [] []放置,并在运行时创建表:

public class SimpleTextTableWithBorders extends TableLayout {

private Context mContext;
private String[][] mTableContent;
private int mTextColor, mBorderColor;
private int mTextViewBorderWidth, mTableBorderWidth;

public SimpleTextTableWithBorders(Context context, String[][] tableContent) {
    super(context);
    mContext = context;
    mTableContent = tableContent;
    mTextColor = 0xff111111;
    mBorderColor = 0xAA444444;
    mTextViewBorderWidth = 4;
    mTableBorderWidth = mTextViewBorderWidth * 2;
    setupTable();
}

private void setupTable() {
    TableRow tableRow;
    TextView textView;

    setStretchAllColumns(true);
    setBackground(borderDrawable(mTableBorderWidth));
    setPadding(mTableBorderWidth, mTableBorderWidth, mTableBorderWidth, mTableBorderWidth);

    for (int currentRow = 0; currentRow < mTableContent.length; currentRow++) {
        tableRow = new TableRow(mContext);

        for (int currentColumn = 0; currentColumn < mTableContent[0].length; currentColumn++) {
            textView = new TextView(mContext);
            textView.setTextColor(mTextColor);
            textView.setBackground(borderDrawable(mTextViewBorderWidth));
            textView.setText(mTableContent[currentRow][currentColumn]);
            textView.setGravity(Gravity.CENTER);
            textView.setPadding(0, 6, 0, 6);
            tableRow.addView(textView);
        }
        addView(tableRow);
    }
}

private GradientDrawable borderDrawable(int borderWidth) {
    GradientDrawable shapeDrawable = new GradientDrawable();
    shapeDrawable.setStroke(borderWidth, mBorderColor);
    return shapeDrawable;
}

} }

The key thing what I was looking for was pretty much GradientDrawable. 我一直在寻找的关键东西是GradientDrawable。 There are no setters and getters, but if someone need, you can put it there with other useful methods and use it as you want. 没有设置器和获取器,但是如果有人需要,可以将其与其他有用的方法放在一起,并根据需要使用它。

Here is the example result: 这是示例结果: 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM