java - Android Studio 表格布局中增加列

标签 java android list android-tablayout

我有表格布局代码,我想增加表格列。我是开发和 Android 表格布局领域的新手...... 我想增加表中的列数,因为我正在使用 android 表格布局!!

但是我还是很困惑怎么办!!

这是我的 xml 文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
android:id="@+id/invoices_layout"
tools:context=".MainActivity">
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
<TableLayout
    android:id="@+id/tableInvoices"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="0dp"
    android:stretchColumns="*">
</TableLayout>
</ScrollView>

这是我的主要 Activity Java 文件

public class MainActivity extends AppCompatActivity {
private TableLayout mTableLayout;
ProgressDialog mProgressBar;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mProgressBar = new ProgressDialog(this);
    mTableLayout = (TableLayout) findViewById(R.id.tableInvoices);
    mTableLayout.setStretchAllColumns(true);
    startLoadData();
}
public void startLoadData() {
    mProgressBar.setCancelable(false);
    mProgressBar.setMessage("Fetching Invoices..");
    mProgressBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    mProgressBar.show();
    new LoadDataTask().execute(0);
}
public void loadData() {
    int leftRowMargin=0;
    int topRowMargin=0;
    int rightRowMargin=0;
    int bottomRowMargin = 0;
    int textSize = 0, smallTextSize = 0 , mediumTextSize = 0;
    textSize = (int) getResources().getDimension(R.dimen.font_size_verysmall);
    smallTextSize = (int) getResources().getDimension(R.dimen.font_size_small);
    mediumTextSize = (int) getResources().getDimension(R.dimen.font_size_medium);
    Invoices invoices = new Invoices();
    InvoiceData[] data = invoices.getInvoices();
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMM, yyyy");
    DecimalFormat decimalFormat = new DecimalFormat("0.00");

    int rows = data.length;
    getSupportActionBar().setTitle("Invoices (" + String.valueOf(rows) + ")");

    TextView textSpacer = null;
    mTableLayout.removeAllViews();
            for(int i = -1; i < rows; i ++) {
        InvoiceData row = null;
        if (i > -1)
        row = data[i];
        else {
            textSpacer = new TextView(this);
            textSpacer.setText("");
        }
        final TextView tv = new TextView(this);
        tv.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
                TableRow.LayoutParams.WRAP_CONTENT));
        tv.setGravity(Gravity.LEFT);
        tv.setPadding(5, 15, 0, 15);

        if (i == -1) {
            tv.setText("Inv.#");
            tv.setBackgroundColor(Color.parseColor("#f0f0f0"));
            tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, smallTextSize);
        } else {
            tv.setBackgroundColor(Color.parseColor("#f8f8f8"));
            tv.setText(String.valueOf(row.invoiceNumber));
            tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
        }

        final TextView tv2 = new TextView(this);
        if (i == -1) {
            tv2.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
                    TableRow.LayoutParams.WRAP_CONTENT));
            tv2.setTextSize(TypedValue.COMPLEX_UNIT_PX, smallTextSize);
        } else {
            tv2.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
                    TableRow.LayoutParams.MATCH_PARENT));
            tv2.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
        }

        tv2.setGravity(Gravity.LEFT);
        tv2.setPadding(5, 15, 0, 15);
        if (i == -1) {
            tv2.setText("Date");
            tv2.setBackgroundColor(Color.parseColor("#f7f7f7"));
        }else {
            tv2.setBackgroundColor(Color.parseColor("#ffffff"));
            tv2.setTextColor(Color.parseColor("#000000"));
            tv2.setText(dateFormat.format(row.invoiceDate));
        }
        final LinearLayout layCustomer = new LinearLayout(this);
        layCustomer.setOrientation(LinearLayout.VERTICAL);
        layCustomer.setPadding(0, 10, 0, 10);
        layCustomer.setBackgroundColor(Color.parseColor("#f8f8f8"));
        final TextView tv3 = new TextView(this);
        if (i == -1) {
            tv3.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
                    TableRow.LayoutParams.MATCH_PARENT));
            tv3.setPadding(5, 5, 0, 5);
            tv3.setTextSize(TypedValue.COMPLEX_UNIT_PX, smallTextSize);
        } else {
            tv3.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
                    TableRow.LayoutParams.MATCH_PARENT));
            tv3.setPadding(5, 0, 0, 5);
            tv3.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
        }
        tv3.setGravity(Gravity.TOP);
        if (i == -1) {
            tv3.setText("Customer");
            tv3.setBackgroundColor(Color.parseColor("#f0f0f0"));
        } else {

            tv3.setBackgroundColor(Color.parseColor("#f8f8f8"));
            tv3.setTextColor(Color.parseColor("#000000"));
            tv3.setTextSize(TypedValue.COMPLEX_UNIT_PX, smallTextSize);
            tv3.setText(row.customerName);
        }

        layCustomer.addView(tv3);
        if (i > -1) {
            final TextView tv3b = new TextView(this);
            tv3b.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
                    TableRow.LayoutParams.WRAP_CONTENT));
            tv3b.setGravity(Gravity.RIGHT);
            tv3b.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
            tv3b.setPadding(5, 1, 0, 5);
            tv3b.setTextColor(Color.parseColor("#aaaaaa"));
            tv3b.setBackgroundColor(Color.parseColor("#f8f8f8"));
            tv3b.setText(row.customerAddress);
            layCustomer.addView(tv3b);
        }

        final LinearLayout layAmounts = new LinearLayout(this);
        layAmounts.setOrientation(LinearLayout.VERTICAL);
        layAmounts.setGravity(Gravity.RIGHT);
        layAmounts.setPadding(0, 10, 0, 10);
        layAmounts.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
                TableRow.LayoutParams.MATCH_PARENT));
        final TextView tv4 = new TextView(this);
        if (i == -1) {
            tv4.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
                    TableRow.LayoutParams.MATCH_PARENT));
            tv4.setPadding(5, 5, 1, 5);
            layAmounts.setBackgroundColor(Color.parseColor("#f7f7f7"));
        } else {
            tv4.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
                    TableRow.LayoutParams.WRAP_CONTENT));
            tv4.setPadding(5, 0, 1, 5);
            layAmounts.setBackgroundColor(Color.parseColor("#ffffff"));
        }
        tv4.setGravity(Gravity.RIGHT);
        if (i == -1) {
            tv4.setText("Inv.Amount");
            tv4.setBackgroundColor(Color.parseColor("#f7f7f7"));
            tv4.setTextSize(TypedValue.COMPLEX_UNIT_PX, smallTextSize);
        } else {
            tv4.setBackgroundColor(Color.parseColor("#ffffff"));
            tv4.setTextColor(Color.parseColor("#000000"));
            tv4.setText(decimalFormat.format(row.invoiceAmount));
            tv4.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
        }
        layAmounts.addView(tv4);
        if (i > -1) {
            final TextView tv4b = new TextView(this);
            tv4b.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
                    TableRow.LayoutParams.WRAP_CONTENT));
            tv4b.setGravity(Gravity.RIGHT);
            tv4b.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
            tv4b.setPadding(2, 2, 1, 5);
            tv4b.setTextColor(Color.parseColor("#00afff"));
            tv4b.setBackgroundColor(Color.parseColor("#ffffff"));
            String due = "";
            if (row.amountDue.compareTo(new BigDecimal(0.01)) == 1) {
                due = "Due:" + decimalFormat.format(row.amountDue);
                due = due.trim();
            }
            tv4b.setText(due);
            layAmounts.addView(tv4b);
        }
        // add table row
        final TableRow tr = new TableRow(this);
        tr.setId(i + 1);
        TableLayout.LayoutParams trParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,
                TableLayout.LayoutParams.WRAP_CONTENT);
        trParams.setMargins(leftRowMargin, topRowMargin, rightRowMargin, bottomRowMargin);
        tr.setPadding(0,0,0,0);
        tr.setLayoutParams(trParams);
        tr.addView(tv);
        tr.addView(tv2);
        tr.addView(layCustomer);
        tr.addView(layAmounts);

        if (i > -1) {
            tr.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    TableRow tr = (TableRow) v;

                }
            });
        }
        mTableLayout.addView(tr, trParams);
        if (i > -1) {
            // add separator row
            final TableRow trSep = new TableRow(this);
            TableLayout.LayoutParams trParamsSep = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,
                    TableLayout.LayoutParams.WRAP_CONTENT);
            trParamsSep.setMargins(leftRowMargin, topRowMargin, rightRowMargin, bottomRowMargin);
            trSep.setLayoutParams(trParamsSep);
            TextView tvSep = new TextView(this);

            TableRow.LayoutParams tvSepLay = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
                    TableRow.LayoutParams.WRAP_CONTENT);
            tvSepLay.span = 4;
            tvSep.setLayoutParams(tvSepLay);

            tvSep.setBackgroundColor(Color.parseColor("#d9d9d9"));
            tvSep.setHeight(1);
            trSep.addView(tvSep);
            mTableLayout.addView(trSep, trParamsSep);

        }
    }
}


 class LoadDataTask extends AsyncTask<Integer, Integer, String> {

        @Override
        protected String doInBackground(Integer... params) {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "Task Completed.";
        }

        @Override
        protected void onPostExecute(String result) {
            mProgressBar.hide();
            loadData();
                 }
        @Override
        protected void onPreExecute() {

        }
        @Override
        protected void onProgressUpdate(Integer... values) {
        }
    }

}

这是我的模型类

    public class InvoiceData {

    public int id;
    public int invoiceNumber;
    public Date invoiceDate;
    public String customerName;
    public String customerAddress;
    public BigDecimal invoiceAmount;
    public BigDecimal amountDue;

}

这是我的发票类

 public class Invoices {

    public InvoiceData[] getInvoices() {
        InvoiceData[] data = new InvoiceData[20];
        for(int i = 0; i < 20; i ++) {
            InvoiceData row = new InvoiceData();
            row.id = (i+1);
            row.invoiceNumber = row.id;
            row.amountDue = BigDecimal.valueOf(20.00 * i);
            row.invoiceAmount = BigDecimal.valueOf(120.00 * (i+1));
            row.invoiceDate = new Date();
            row.customerName =  "Thomas John Beckett";
            row.customerAddress = "1112, Hash Avenue, NYC";
            //row.Code= "1233";
            data[i] = row;
        }
        return data;
    }

}

我想做的是增加表中的列数...

添加更多不显示小屏幕的列

Image

我有 8 列,但只显示 6

最佳答案

您可以查看Blackie answer这里了解如何使用xml实现TableLayout中的列数。
对于您的示例,您可以在类似这样的情况下使用它:

        <TableLayout
                android:id="@+id/tableInvoices"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:padding="0dp"
                android:stretchColumns="7">
        <TableRow>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_column="1" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_column="1"/>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_column="1" />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_column="1" />
               <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_column="1" />
              <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_column="1" />
              <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_column="1" />

        </TableRow>
   </TableLayout>

更新:

对于较小的水平尺寸,您有多种解决方案:

1- 对标题和所有其他 TextView 使用较小的 textSize 并使用 wrap_content
2-使用LinearLayout代替TableLayout,并为每列指定layout_weight。
3-使用水平ScrollView。

关于java - Android Studio 表格布局中增加列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55969289/

相关文章:

java - generic varargs 的编译器警告的解决方案

java - 解析具有任意名称的 JSON 节点

python - 列表大小不同

java - 从 XML Schema 数据类型转换为 Java int

java - 截图

android - 在 Android 上拖动以动态调整一对相邻布局的大小

java - 在 Android 中设置委托(delegate)

python - 查找最大数 <= 其他一些数

list - 在有限集上定义一个类似于 "arg max"的函数,并证明它的一些属性,并避免通过列表绕道而行

java - 实现TextureView时按钮不显示