动态TableLayout内的TextView不显示全文

时间:2018-02-21 12:04:11

标签: android user-interface textview tablelayout

我正在尝试在Android中显示动态表格。 我发现在XML中创建TableLayout和TableHeader(第一行),然后动态创建TableRows并不能很好地工作。 所以我只在XML中创建了一个TableLayout,我使用他的id在我的java文件中创建一个新行。

这是我的XML布局:在SensorTable之前还有另外两个TableLayout。

<android.support.constraint.ConstraintLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="4dp">
 <TableLayout
        android:id="@+id/SensorTable"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="4dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/eaccessresulttable">
    </TableLayout>
</android.support.constraint.ConstraintLayout>

然后我的行包含以下java代码

View verticalline = new View(mcontext);
    verticalline.setLayoutParams(new TableRow.LayoutParams(1, TableRow.LayoutParams.MATCH_PARENT));
    verticalline.setBackgroundColor(getResources().getColor(R.color.black));

    LinearLayout trheader = new LinearLayout(mcontext);
    trheader.setLayoutParams(new TableLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
    trheader.setBackgroundColor(getResources().getColor(R.color.color_jaune_logo));
    trheader.setWeightSum(4.2f);

    TextView SensorNumberHeaderTextView = new TextView(mcontext);
    TextView SensorProblemHeaderTextView = new TextView(mcontext);
    TextView SensorTypeHeaderTextView = new TextView(mcontext);

    SensorNumberHeaderTextView.setText("Sensor n°");
    SensorProblemHeaderTextView.setText("Diagnosis");
    SensorTypeHeaderTextView.setText("Type");
    SensorNumberHeaderTextView.setTextColor(getResources().getColor(R.color.blanc));
    SensorProblemHeaderTextView.setTextColor(getResources().getColor(R.color.blanc));
    SensorTypeHeaderTextView.setTextColor(getResources().getColor(R.color.blanc));

    SensorNumberHeaderTextView.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f));
    SensorTypeHeaderTextView.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1.2f));
    SensorProblemHeaderTextView.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 2.0f));

    trheader.addView(SensorNumberHeaderTextView);
    trheader.addView(verticalline);
    verticalline = new View(mcontext);
    verticalline.setLayoutParams(new TableRow.LayoutParams(1, TableRow.LayoutParams.MATCH_PARENT));
    verticalline.setBackgroundColor(getResources().getColor(R.color.black));

    trheader.addView(SensorTypeHeaderTextView);
    verticalline = new View(mcontext);
    verticalline.setLayoutParams(new TableRow.LayoutParams(1, TableRow.LayoutParams.MATCH_PARENT));
    verticalline.setBackgroundColor(getResources().getColor(R.color.black));
    trheader.addView(verticalline);

    trheader.addView(SensorProblemHeaderTextView);
    verticalline = new View(mcontext);
    verticalline.setLayoutParams(new TableRow.LayoutParams(1, TableRow.LayoutParams.MATCH_PARENT));
    verticalline.setBackgroundColor(getResources().getColor(R.color.black));
    trheader.addView(verticalline);

    View horizontalLine = new View(mcontext);
    horizontalLine.setLayoutParams(new TableRow.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 1));
    horizontalLine.setBackgroundColor(getResources().getColor(R.color.black));
    tl.addView(horizontalLine);
    tl.addView(trheader);

    horizontalLine = new View(mcontext);
    horizontalLine.setLayoutParams(new TableRow.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 1));
    horizontalLine.setBackgroundColor(getResources().getColor(R.color.black));
    tl.addView(horizontalLine);

    int i = 0;
    while (i < sensorDiags.length)
    {
        LinearLayout tr = new LinearLayout(mcontext);
        tr.setLayoutParams(new TableLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
        tr.setWeightSum(4.2f);
        if ((i % 2) == 0)
        {
            tr.setBackgroundColor(getResources().getColor(R.color.gris_clair));
        }
        else {
            tr.setBackgroundColor(getResources().getColor(R.color.blanc));
        }

        TextView SensorNumberTextView = new TextView(mcontext);
        TextView SensorProblemTextView = new TextView(mcontext);
        TextView SensorTypetextView = new TextView(mcontext);

        SensorNumberTextView.setText(String.valueOf(sensorDiags[i].getmSensorNumber()));
        SensorProblemTextView.setText(sensorDiags[i].getmDiagnosis());
        SensorTypetextView.setText(sensorDiags[i].getmType());

        SensorNumberHeaderTextView.setSingleLine(false);
        SensorProblemTextView.setSingleLine(false);
        SensorTypetextView.setSingleLine(false);

        SensorNumberTextView.setTextColor(getResources().getColor(R.color.black));
        SensorProblemTextView.setTextColor(getResources().getColor(R.color.black));
        SensorTypetextView.setTextColor(getResources().getColor(R.color.black));

        SensorNumberTextView.setLayoutParams(new TableRow.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f));
        SensorTypetextView.setLayoutParams(new TableRow.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1.2f));
        SensorProblemTextView.setLayoutParams(new TableRow.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 2.0f));

        verticalline = new View(mcontext);
        verticalline.setLayoutParams(new TableRow.LayoutParams(1, TableRow.LayoutParams.MATCH_PARENT));
        verticalline.setBackgroundColor(getResources().getColor(R.color.black));
        tr.addView(verticalline);

        tr.addView(SensorNumberTextView);
        verticalline = new View(mcontext);
        verticalline.setLayoutParams(new TableRow.LayoutParams(1, TableRow.LayoutParams.MATCH_PARENT));
        verticalline.setBackgroundColor(getResources().getColor(R.color.black));
        tr.addView(verticalline);

        tr.addView(SensorTypetextView);
        verticalline = new View(mcontext);
        verticalline.setLayoutParams(new TableRow.LayoutParams(1, TableRow.LayoutParams.MATCH_PARENT));
        verticalline.setBackgroundColor(getResources().getColor(R.color.black));
        tr.addView(verticalline);

        tr.addView(SensorProblemTextView);
        verticalline = new View(mcontext);
        verticalline.setLayoutParams(new TableRow.LayoutParams(1, TableRow.LayoutParams.MATCH_PARENT));
        verticalline.setBackgroundColor(getResources().getColor(R.color.black));
        tr.addView(verticalline);

        tl.addView(tr);
        horizontalLine = new View(mcontext);
        horizontalLine.setLayoutParams(new TableRow.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 1));
        horizontalLine.setBackgroundColor(getResources().getColor(R.color.black));
        i++;
    }

这里有两个问题。首先,正如你所看到的,我创建了很多垂直线和水平线来与我的桌子接壤。 有没有一种正确的方法可以在不创建View的情况下完成此操作 第二,我的单元格中的文本没有完全显示,我想我错过了像MATCH_PARENT而不是WRAP_CONTENT的东西,但我无法看到。

MyLayout

enter image description here

有没有人可以解决这个问题?

0 个答案:

没有答案