【问题标题】:Increase Column in Table Layout Android Studio增加表格布局Android Studio中的列
【发布时间】:2019-05-27 17:30:26
【问题描述】:

我有表格布局代码,我想增加表格列。我是开发和 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>

这是我的主要活动 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;
    }

}

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

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

我有 8 列,但它只显示 6

【问题讨论】:

    标签: java android list android-tablayout


    【解决方案1】:

    您可以查看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- 使用水平滚动视图。

    【讨论】:

    • 我想获取数据库类中的 Sqlite 数据
    • 对于 Sqlite 数据库中的数据,您需要创建一个数据库助手类,然后您还需要一个适配器用于您的 recyclerview(显示它们),您可以在这里找到完整的教程:@987654322 @
    • 添加更多列后其他项目未显示
    • 还有哪些项目?你可以让它 wrap_content 只占用它需要的空间。在您的情况下,您只需将适配器项更改为此表格布局,所有其他都相同,但确定并不像预期的那样容易,只需要一些练习。
    • @AliceAlex 如果您将文本大小设置得非常小并且这不是一个好习惯,我可以建议您在单击包含其他数据的行时显示几列并弹出一个(对话框) , 这是智能手机的最佳做法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多