Textview和TableRow Weights工作不正常Android



我一辈子都不明白为什么我的文本会变成这样。我是java/android编程的新手,所以如果答案对我来说不明显,我很抱歉。我有一个tablelayout,其中有一个静态行,其余的都是动态编程的。静态行看起来不错,但我的动态行的第一个textview被压扁了,尽管两个textviews的权重都设置为1。有人能告诉我我在做什么/不在做什么吗?

XML:

<TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="16dp"
        android:id="@+id/alchemist_favored_class">
        <TableRow>
            <TextView
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="Race"
                android:gravity="left"
                android:padding="2dp" />
            <TextView
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="Bonus"
                android:gravity="left"
                android:padding="2dp" />
        </TableRow>
    </TableLayout>

Java:

//Populate Favored Class Options Table
    TableLayout tableFC = (TableLayout) findViewById(R.id.alchemist_favored_class);
    String[] fcr = getResources().getStringArray(R.array.alchemist_favored_class_race);
    String[] fcd = getResources().getStringArray(R.array.alchemist_favored_class_detail);
    TableRow.LayoutParams lp1 = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 1f);
    TableRow.LayoutParams lp2 = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 1f);
    for (int i = 0; i < fcr.length; i++){
        TableRow tr = new TableRow(this);
        TextView tvFCR = new TextView(this);
        TextView tvFCD = new TextView(this);
        tr.addView(tvFCR);
        tvFCR.setText(fcr[i]);
        tvFCR.setSingleLine(true);
        tvFCR.setPadding(2,2,2,2);
        tvFCR.setLayoutParams(lp1);
        tr.addView(tvFCD);
        tvFCD.setText(fcd[i]);
        tvFCD.setPadding(2,2,2,2);
        tvFCD.setLayoutParams(lp2);
        //Add background color to every other row.
        if ((i % 2) == 0) {
            tr.setBackgroundColor(Color.parseColor("#EEEEEE"));
        }
        tableFC.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT));
    }

结果:

Imgur

放一些类似的东西--

tableFC.setStretchAllColumns(true);

希望这能有所帮助!

从行中的第一个文本视图中删除权重解决了问题,现在根据需要显示。

图像

最新更新