从代码android修改列宽

时间:2015-09-24 12:26:04

标签: android xml android-layout android-tablelayout layoutparams

我想知道是否有办法修改表格布局中两列的宽度(总共有三列)。

问题是每个列都有相同的宽度,浪费了大量的空间,因为前两个只包含一个id和一个日期,而最后一个包含多行描述。

这是填充表格布局的代码:

tr_head.setLayoutParams(new LayoutParams(
    LayoutParams.MATCH_PARENT,
    LayoutParams.WRAP_CONTENT));

    tr_head.setBackgroundColor(Color.rgb(0, 230, 230));

    TextView lblSystemStoricalHeaderId = new TextView(this);
    lblSystemStoricalHeaderId.setText("ID      ");

    tr_head.addView(lblSystemStoricalHeaderId);

    TextView lblSystemStoricalHeaderData = new TextView(this);
    lblSystemStoricalHeaderData.setText("Data");

    tr_head.addView(lblSystemStoricalHeaderData);

    TextView lblSystemStoricalHeaderDescription = new TextView(this);
    lblSystemStoricalHeaderDescription.setText("      Descrizione");

    tr_head.addView(lblSystemStoricalHeaderDescription);

    tr_head.setMinimumHeight(30);

    tlSystemStorical.addView(tr_head, new TableLayout.LayoutParams(
       LayoutParams.MATCH_PARENT,
           LayoutParams.WRAP_CONTENT));

    for(int i=0;i<reportList.length;i++){

        TableRow tr = new TableRow(this);
        tr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT));

        TextView lblSystemStoricalId = new TextView(this);
        lblSystemStoricalId.setText(""+reportList[i].getId()+"");

        tr.addView(lblSystemStoricalId);

        TextView lblSystemStoricalData = new TextView(this);
        long dataC = reportList[i].getDate()*1000;
        java.util.Date df = new java.util.Date(dataC);
        String vv = new SimpleDateFormat("dd/MM/yyyy").format(df);
        lblSystemStoricalData.setText(vv);

        tr.addView(lblSystemStoricalData);

        TableRow.LayoutParams tv3Params = new TableRow.LayoutParams();
        tv3Params.width=0;
        tv3Params.weight=1;
        TextView lblSystemStoricalDescription = new TextView(this);
        lblSystemStoricalDescription.setText(""+reportList[i].getReportDescription());
        lblSystemStoricalDescription.setLayoutParams(tv3Params);

        tr.addView(lblSystemStoricalDescription);

        tr.setBackgroundResource(R.drawable.border);
        tr.setMinimumHeight(40);

        tlSystemStorical.addView(tr, new TableLayout.LayoutParams(
                LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT));
    }

这是xml文件:

<ScrollView 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/scroll_view_system_description"
     >

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
         >

        <TableLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:stretchColumns="*"
            android:id="@+id/tl_system_storical"
            >

        </TableLayout>
    </FrameLayout>

</ScrollView>

1 个答案:

答案 0 :(得分:0)

您可以为width子项设置0属性为TableRow,但为其指定layout_weight。所以你要写:

float[] weights = new float[] { 0.2f, 0.2f, 0.6f};

TextView lblSystemStoricalId = new TextView(this);
lblSystemStoricalId.setLayoutParams(new TableRow.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, weights[0]))

其中0没有视图宽度(因为您正在使用权重),而weights数组包含三个视图的权重。

这些权重应总计为1,每个权重是列应采用的宽度的百分比。因此,您需要为TableRow个孩子分配权重 - lblSystemStoricalId获得weights[0]lblSystemStoricalData获得weights[1]lblSystemStoricalDescription拥有weights[2] #include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc, char *argv[]) { int i; char *str_result; char *str_dst = NULL; str_result = (char *) malloc(100); str_dst = (char *) malloc(100); str_result = "Action done"; size_t len = strlen(str_result); printf("string length is = %d\n", len); memcpy(str_dst, str_result, len); str_dst [len] = '\0'; for (i = 0; i < len; i++) { printf("%s\n", str_dst[i]); } free(str_dst); free(str_result); return 0; } 1}}。