【问题标题】:Column of same width in TableLayout, *programmatically*TableLayout 中相同宽度的列,*以编程方式*
【发布时间】:2018-12-23 21:12:29
【问题描述】:

我在 SO 上读到,为了获得每列具有相同宽度的表格布局,您可以将 android:width="0dp"android:weight="1" 设置为布局参数并且它可以工作。 我会得到相同的结果,但以编程方式,所以我尝试了这段代码:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_score, container, false);

    TableRow row = (TableRow)rootView.findViewById(R.id.playerRow);
    for (Player p : game.getPlayers()) {
        TextView t = new TextView(rootView.getContext());
        t.setText(p.getName());
        row.addView(t, new TableLayout.LayoutParams(0, TableLayout.LayoutParams.WRAP_CONTENT, 1f));
        // with the previuos line of code, nothing is showed
        // instead this work: row.addView(t) , but each column doesn't take the maximum width (as I would)

    }

    return rootView;
}

但正如评论中所解释的,它没有按预期工作。 我无法得到我缺少的东西。

【问题讨论】:

  • 试试这个:row.addView(t, new TableLayout.LayoutParams(parent.getWidth()/numColumns, TableLayout.LayoutParams.WRAP_CONTENT, 0)); where numColumns == 表中的列数
  • 同样的结果。我试过row.addView(t, new TableLayout.LayoutParams(row.getWidth()/game.getPlayers().size(), TableLayout.LayoutParams.WRAP_CONTENT, 1f));,没有任何改变

标签: android android-layout


【解决方案1】:

我的代码有错误,LayoutParams 是错误的类,我应该使用TableRow.LayoutParams 而不是TableLayout.LayoutParams,所以:

row.addView(t, new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 1f));

按预期工作。

【讨论】:

    【解决方案2】:

    你也可以试试这个,特别是如果你想在表格单元格之间设置边距。

    TableRow.LayoutParams tableRowParams = new TableRow.LayoutParams(0,TableRow.LayoutParams.WRAP_CONTENT,1f);
    tableRowParams.setMargins(5,5,5,5);
    row.addView(t, tableRowParams);
    

    对于统一的列分布,必须在您的 xml 文件中的 TableLayout 中添加 android:stretchColumns="*"

    【讨论】:

      【解决方案3】:

      你可以这样试试。

      <?xml version="1.0" encoding="utf-8"?>
      <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/tableLayout1"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent" >
      
          <!-- 2 columns -->
          <TableRow
              android:id="@+id/tableRow1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:padding="5dip" >
      
              <TextView
                  android:id="@+id/textView1"
                  android:text="Column 1"
                  android:textAppearance="?android:attr/textAppearanceLarge" />
      
              <Button
                  android:id="@+id/button1"
                  android:text="Column 2" />
          </TableRow>
      
          <!-- edittext span 2 column -->
          <TableRow
              android:id="@+id/tableRow2"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:padding="5dip" >
      
              <EditText
                  android:id="@+id/editText1"
                  android:layout_span="2"
                  android:text="Column 1 &amp; 2" />
          </TableRow>
      
          <!-- just draw a red line -->
          <View
              android:layout_height="2dip"
              android:background="#FF0000" />
      
          <!-- 3 columns -->
          <TableRow
              android:id="@+id/tableRow3"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:padding="5dip" >
      
              <TextView
                  android:id="@+id/textView2"
                  android:text="Column 1"
                  android:textAppearance="?android:attr/textAppearanceLarge" />
      
              <Button
                  android:id="@+id/button2"
                  android:text="Column 2" />
      
              <Button
                  android:id="@+id/button3"
                  android:text="Column 3" />
          </TableRow>
      
          <!-- display this button in 3rd column via layout_column(zero based) -->
          <TableRow
              android:id="@+id/tableRow4"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:padding="5dip" >
      
              <Button
                  android:id="@+id/button4"
                  android:layout_column="2"
                  android:text="Column 3" />
          </TableRow>
      
          <!-- display this button in 2nd column via layout_column(zero based) -->
          <TableRow
              android:id="@+id/tableRow5"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:padding="5dip" >
      
              <Button
                  android:id="@+id/button5"
                  android:layout_column="1"
                  android:text="Column 2" />
          </TableRow>
      
      </TableLayout>
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-13
      • 1970-01-01
      • 2010-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多