温馨提示×

在Android中如何设置datagrid的列宽

小樊
82
2024-09-07 22:11:39
栏目: 编程语言

在 Android 中,没有名为 DataGrid 的组件

  1. 使用 XML 布局文件:

在 XML 布局文件中,可以为每个列指定宽度。例如:

    android:layout_width="match_parent"
    android:layout_height="wrap_content">

   <TableRow>
       <TextView
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="Column 1"/>

       <TextView
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:text="Column 2"/>
    </TableRow>

</TableLayout>
  1. 使用 Java 代码:

在 Java 代码中,可以通过设置 TableRow 和 TextView 的 LayoutParams 来设置列宽。例如:

TableLayout tableLayout = new TableLayout(this);
TableRow tableRow = new TableRow(this);

TextView textView1 = new TextView(this);
textView1.setText("Column 1");
TableRow.LayoutParams layoutParams1 = new TableRow.LayoutParams(100, ViewGroup.LayoutParams.WRAP_CONTENT);
textView1.setLayoutParams(layoutParams1);
tableRow.addView(textView1);

TextView textView2 = new TextView(this);
textView2.setText("Column 2");
TableRow.LayoutParams layoutParams2 = new TableRow.LayoutParams(200, ViewGroup.LayoutParams.WRAP_CONTENT);
textView2.setLayoutParams(layoutParams2);
tableRow.addView(textView2);

tableLayout.addView(tableRow);

这两种方法都可以实现设置 DataGrid(或者说 TableLayout)的列宽。请根据您的需求选择合适的方法。

0