如何以编程方式设置跨多行的子视图

时间:2014-02-10 22:30:10

标签: android xamarin android-gridlayout

运行此活动时,我收到了非法参数异常。有人能指出为什么吗?我对规范的评论有望引起你对这段代码的误解:

[Activity(Label = "Views/Layouts/GridLayout/3. Form (Java)", MainLauncher = true)]
  [IntentFilter(new[] { Intent.ActionMain }, Categories = new string[] { })]
  public class GridLayout3 : Activity {
    protected override void OnCreate(Bundle bundle) {
      base.OnCreate(bundle);


      SetContentView(Grid(this));
      //SetContentView (Create (this));
    }

    public static View Grid(Context context) {
      GridLayout gL = new GridLayout(context);
      gL.AlignmentMode = GridAlign.Bounds;
      gL.UseDefaultMargins = true;
      //gL.ColumnOrderPreserved = false;
      //gL.RowOrderPreserved = false;
      gL.RowCount = 5;
      gL.ColumnCount = 5;

      //I want these Specs to tell the View to take up 2 rows of space. 
      //IE, (row 0-indexed) of 3 and 4 in column 4 should contain 
      //the same button
      GridLayout.Spec specialRowSpec = GridLayout.InvokeSpec(3, 4);
      GridLayout.Spec specialColSpec = GridLayout.InvokeSpec(4);

      bool keyIsSpecial = false;
      for (int i = 0; i < gL.RowCount; i++) {
        GridLayout.Spec row = GridLayout.InvokeSpec(i);

        for (int v = 0; v < gL.ColumnCount; v++) {
          GridLayout.Spec col = GridLayout.InvokeSpec(v);
          Button bt = new Button(context);
          bt.Text = "Tester";
          if (i == 3 && v == 4) {
            keyIsSpecial = true;
          }
          if (keyIsSpecial) {
            GridLayout.LayoutParams param = new GridLayout.LayoutParams();
            param.RowSpec = specialRowSpec;
            param.ColumnSpec = specialColSpec;
            param.Height = 100;
            gL.AddView(bt, new GridLayout.LayoutParams(param));
            return gL;
          } else {
            gL.AddView(bt, new GridLayout.LayoutParams(row, col));
          }
          if (v == gL.ColumnCount - 1) {
            v = 0;
            break;
          }
        }
      }
      return gL;
    }

当我使用特殊规范添加视图时抛出异常:[Activity(Label = "Views/Layouts/GridLayout/3. Form (Java)", MainLauncher = true)] [IntentFilter(new[] { Intent.ActionMain }, Categories = new string[] { })] public class GridLayout3 : Activity { protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); SetContentView(Grid(this)); //SetContentView (Create (this)); } public static View Grid(Context context) { GridLayout gL = new GridLayout(context); gL.AlignmentMode = GridAlign.Bounds; gL.UseDefaultMargins = true; //gL.ColumnOrderPreserved = false; //gL.RowOrderPreserved = false; gL.RowCount = 5; gL.ColumnCount = 5; //I want these Specs to tell the View to take up 2 rows of space. //IE, (row 0-indexed) of 3 and 4 in column 4 should contain //the same button GridLayout.Spec specialRowSpec = GridLayout.InvokeSpec(3, 4); GridLayout.Spec specialColSpec = GridLayout.InvokeSpec(4); bool keyIsSpecial = false; for (int i = 0; i < gL.RowCount; i++) { GridLayout.Spec row = GridLayout.InvokeSpec(i); for (int v = 0; v < gL.ColumnCount; v++) { GridLayout.Spec col = GridLayout.InvokeSpec(v); Button bt = new Button(context); bt.Text = "Tester"; if (i == 3 && v == 4) { keyIsSpecial = true; } if (keyIsSpecial) { GridLayout.LayoutParams param = new GridLayout.LayoutParams(); param.RowSpec = specialRowSpec; param.ColumnSpec = specialColSpec; param.Height = 100; gL.AddView(bt, new GridLayout.LayoutParams(param)); return gL; } else { gL.AddView(bt, new GridLayout.LayoutParams(row, col)); } if (v == gL.ColumnCount - 1) { v = 0; break; } } } return gL; }

以下是我可能误解的GridLayout.Spec文档的链接:http://developer.android.com/reference/android/widget/GridLayout.Spec.html

我也尝试将第二个参数交换到我的specialRowSpec,以使用总大小(高度)而不是行的索引。例如:gL.AddView(bt, new GridLayout.LayoutParams(param));这只是给我一个5乘5的按钮网格,没有按钮跨越多行。

有问题吗?

0 个答案:

没有答案