【问题标题】:android tablelayout expand row height to listview contentsandroid tablelayout将行高扩展到listview内容
【发布时间】:2014-03-17 16:17:21
【问题描述】:

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

以下代码在一个微小的垂直可滚动视图中显示列表视图,与第一列中的字段名称高度相同。如何扩展行高以便查看整个列表? (表格单元格中的 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);

【问题讨论】:

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


    【解决方案1】:

    从来没有让这个列表视图工作。改用线性布局...

    // 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>
    

    【讨论】:

      猜你喜欢
      • 2018-11-13
      • 2021-06-11
      • 2015-08-08
      • 1970-01-01
      • 2013-07-23
      • 2012-08-28
      • 1970-01-01
      • 2013-03-23
      • 1970-01-01
      相关资源
      最近更新 更多