0

首先,我的所有组件都必须在运行时创建。好吧,我有一个TableLayout,在这个表视图里面有一堆TableRow. 在 everyTableRow中,应该有:

  • aButton在右侧具有给定的固定高度和给定的固定宽度
  • a填充被允许包装其文本TextView的“其余部分”TableRow

的高度TableRow应该是Button高度和TableRow高度的最大值。我要么未能设置Button高度,要么只显示其中一个元素 (TableRowButton)。

也许有人可以帮助我。

我当前的代码:

    // fetching the table layout
    final TableLayout tl = (TableLayout) findViewById(R.id.ShoppingListTableLayout);
    tl.removeAllViews();

    // creating a single layout for every component
    TableLayout.LayoutParams tableRowParams = new TableLayout.LayoutParams(
        TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.MATCH_PARENT);
    tableRowParams.setMargins(0, 1, 0, 1);

    TableRow.LayoutParams bParams = new TableRow.LayoutParams(m_Resources
        .getDimensionPixelSize(R.dimen.ButtonWidth), m_DefaultButtonHeight_px);

    TableLayout.LayoutParams tvParams = new TableLayout.LayoutParams(
        TableLayout.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);

    Iterator<xy> itr = ...

    while (itr.hasNext()) {
      final xy = itr.next();
      final TableRow tr = new TableRow(this);
      tr.setLayoutParams(tableRowParams);

      // 1. Child: TextView
      final TextView CurrTxtView = new TextView(this);
      CurrTxtView.setPadding(TV_PADDING_PX, TV_PADDING_PX, TV_PADDING_PX, TV_PADDING_PX);
      CurrTxtView.setTextSize(TV_TEXT_SIZE);
      CurrTxtView.setTypeface(m_Font);
      CurrTxtView.setTextColor(TEXT_ACTIVE_COLOR);
      CurrTxtView.setText(CurrItem.GetName());
      CurrTxtView.setLayoutParams(tvParams);
      tr.addView(CurrTxtView, CHILD_INDEX_TV);

      // 2. Child Button ("Edit" für aktive, "Delete" für inaktive)
      final Button MyButton = new Button(this);
      MyButton.setTextColor(TEXT_ACTIVE_COLOR;
      MyButton.setText(m_Resources.getString(CurrItem.GetIsActive() ? R.string.EditString
          : R.string.DeleteString));
      MyButton.setLayoutParams(bParams);
      MyButton.setTypeface(m_Font);
      MyButton.setOnClickListener(new View.OnClickListener()
      {

        @Override
        public void onClick(View v)
        {... }
      });

      tr.addView(MyButton, CHILD_INDEX_B);

      tr.setPadding(m_TableRowPadding_px, 0, m_TableRowPadding_px, 0);
      tr.setGravity(Gravity.CENTER_VERTICAL);

tl.addView(tr, 0);
    }

在这种情况下,仅显示按钮,左侧更糟!

4

1 回答 1

0

假设您在正确的索引处添加TextViewand (并且是and ,尽管您可以删除它们并以正确的顺序添加视图),布局中的一个坏事是您设置为出现:ButtonCHILD_INDEX_TVCHILD_INDEX_B01LayoutParamsTextView

TableLayout.LayoutParams tvParams = new TableLayout.LayoutParams(
        TableLayout.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);

首先, theTextView是 a 的孩子,TableRow所以你应该设置TableRow.LayoutParamsand not TableLayout.LayoutParams,其次我不明白你为什么突然决定离开 theTableLayout.LayoutParamsandroid.view.ViewGroup.LayoutParams为视图的高度而去。最后应该是:

TableRow.LayoutParams tvParams = new TableRow.LayoutParams(
                TableRow.LayoutParams.MATCH_PARENT,
                TableRow.LayoutParams.WRAP_CONTENT);

要将Button向右推,您可以TextView使用以下方法简单地拉伸列:

tl.setColumnStretchable(0, true);
于 2012-07-17T19:14:30.393 回答