android tablelayout将行高扩展为listview内容

时间:2014-02-18 05:07:51

标签: android android-layout listview android-listview tablelayout

我有一个包含两列的表单的表格布局 - 字段名称和字段值。在某些情况下,字段值是一个清单。我希望“清单”行的行高度展开,以便我可以看到列表视图中的所有项目。

以下代码在一个微小的垂直可滚动视图中显示listview,其高度与第一列中的字段名称相同。如何扩展行高,以便查看整个列表? (表格单元格中的FrameLayout是可以覆盖图标或文本的。)

// get the table
TableLayout table = FindViewById<TableLayout>(Resource.Id.formTable);

// make a new table row
TableRow row = new TableRow(this);

// add the field name (left column)
TextView rowName = new TextView(this);
rowName.Text = field.Name + ":";
rowName.Gravity = GravityFlags.Right | GravityFlags.CenterVertical;
TableRow.LayoutParams tableLayoutParams = new TableRow.LayoutParams(LinearLayout.LayoutParams.FillParent, LinearLayout.LayoutParams.FillParent);
tableLayoutParams.Gravity = GravityFlags.Right | GravityFlags.CenterVertical;
tableLayoutParams.Weight = 1.0f;
tableLayoutParams.Width = 0;
row.AddView(rowName, tableLayoutParams);

// add the field value (right column - in this case, a listview for a checklist)
FrameLayout frameLayout = new FrameLayout(this);

List<string> strings = field.Items.ToList();
ListView listView = new ListView(this);
listView.Adapter = new ListViewFormAdapterCheckList(this, strings);

frameLayout.AddView(listView);
tableLayoutParams = new TableRow.LayoutParams(TableLayout.LayoutParams.WrapContent, TableLayout.LayoutParams.WrapContent);
tableLayoutParams.SetMargins(0, 10, 0, 0);
tableLayoutParams.Width = 0;
row.AddView(frameLayout, tableLayoutParams);

1 个答案:

答案 0 :(得分:0)

从未使用listview。使用线性布局代替......

// get the table
TableLayout table = FindViewById<TableLayout>(Resource.Id.formTable);

// make a new table row
TableRow row = new TableRow(this);

// put the field name at the top of the row
rowName.Gravity = GravityFlags.Right | GravityFlags.Top;
rowName.SetPadding(0, 20, 20, 0);

// frame layout so we can overlay an image / icon over the list
FrameLayout frameLayout = new FrameLayout(this);
frameLayout.Id = kCurrentFieldId++;
frameLayout.Tag = index;
mFieldToView[field] = frameLayout;

List<string> strings = field.Items.ToList();
LinearLayout listLayout = new LinearLayout(this);
listLayout.Orientation = Android.Widget.Orientation.Vertical;

LayoutInflater inflater = (LayoutInflater) GetSystemService(Context.LayoutInflaterService);
foreach (string item in strings)
{
    View v = inflater.Inflate(Resource.Layout.list_checklist, null);
    v.FindViewById<CheckBox>(Resource.Id.checkbox).Text = item;
    listLayout.AddView(v);
}

frameLayout.AddView(listLayout);

tableLayoutParams = new TableRow.LayoutParams(TableLayout.LayoutParams.WrapContent, TableLayout.LayoutParams.WrapContent);
tableLayoutParams.SetMargins(0, 10, 0, 0);
tableLayoutParams.Width = 0;
tableLayoutParams.Weight = 2.0f;
row.AddView(frameLayout, tableLayoutParams);

list_checklist定义为:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="5dip">
    <CheckBox
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:gravity="center_vertical"
        android:text="checkdesc" />
</RelativeLayout>