【问题标题】:how to add two buttons in the same column of a row in table layout?如何在表格布局的同一行的同一列中添加两个按钮?
【发布时间】:2013-06-09 16:27:36
【问题描述】:

我有一个表格布局,我在其中显示来自我的数据库的值我有七列用于该表格.. 我正在以编程方式向表格布局添加行添加数据很好..我通过使用 textviews 来做到这一点..但是在我的第七列中我需要添加两个按钮..我需要在同一列中的两个按钮和一个旁边其他..我只能显示一个按钮..我如何在同一列的一行中显示两个按钮???有可能这样做吗? 请帮忙!! 谢谢!

这是我以编程方式添加行的方式:

do
        {
            tr=new TableRow(this);
            tr.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT));

            firstCol=new TextView(this);
            firstCol.setText("0");
            firstCol.setLayoutParams(new LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));
            firstCol.setGravity(Gravity.CENTER);
            tr.addView(firstCol);

            secondCol=new TextView(this);
            secondCol.setText(lead.getString(index0));
            secondCol.setLayoutParams(new LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));
            secondCol.setGravity(Gravity.CENTER);
            tr.addView(secondCol);

            thirdCol=new TextView(this);
            thirdCol.setText(lead.getString(index1));
            thirdCol.setLayoutParams(new LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));
            thirdCol.setGravity(Gravity.CENTER);
            tr.addView(thirdCol);

            fourthCol=new TextView(this);
            fourthCol.setText(lead.getString(index2));
            fourthCol.setLayoutParams(new LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));
            fourthCol.setGravity(Gravity.CENTER);
            tr.addView(fourthCol);

            fifthCol=new TextView(this);
            fifthCol.setText(lead.getString(index3));
            fifthCol.setLayoutParams(new LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));
            fifthCol.setGravity(Gravity.CENTER);
            tr.addView(fifthCol);

            sixthCol=new TextView(this);
            sixthCol.setText(lead.getString(index4));
            sixthCol.setLayoutParams(new LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));
            sixthCol.setGravity(Gravity.CENTER);
            tr.addView(sixthCol);

            seventhCol=new Button(this);
            seventhCol.setBackground(getApplicationContext().getResources().getDrawable(R.drawable.circ));
            tr.addView(seventhCol);

            lead_table.addView(tr);

            tr.setOnClickListener(this);

        }while(lead.moveToNext());

这是表格布局:

 <TableLayout
        android:id="@+id/lead_table"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="50dp"
        android:gravity="center"
        android:stretchColumns="*" >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:background="@drawable/bgbtn"
                android:paddingLeft="30dp"
                android:text="Lead ID"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/textView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/bgbtn"
                android:paddingLeft="15dp"
                android:text="Name"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/textView4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/bgbtn"
                android:paddingLeft="20dp"
                android:text="Mobile"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/textView5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/bgbtn"
                android:paddingLeft="15dp"
                android:text="Product"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/textView6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/bgbtn"
                android:paddingLeft="15dp"
                android:text="Value"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/textView7"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/bgbtn"
                android:paddingLeft="15dp"
                android:text="Status"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/textView8"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/bgbtn"
                android:paddingLeft="15dp"
                android:text="Action"
                android:textAppearance="?android:attr/textAppearanceMedium" />

        </TableRow>


    </TableLayout>

【问题讨论】:

  • 从逻辑上思考,如果要让它们并排显示,如何在一行的同一列中显示2个按钮?这没有任何逻辑意义。但是,您可以在同一行的两个不同列中保留 2 个按钮,并将它们作为并排实体显示。
  • @TheDarkKnight 但我有该列的标题,这两个按钮需要在该列下方!
  • 好的,所以您需要在该列内添加两列,然后将按钮添加到这些列。让我试试。我会尽快回复您 。或者您也可以遵循 Torben 的解决方案,我认为这也可以。
  • 贴出你的activity_main.xml,这样我们就可以看到表格布局了

标签: android button tablelayout tablerow


【解决方案1】:

创建一个LinearLayout,向其中添加两个按钮,然后将LinearLayout 添加到TableRow

【讨论】:

    猜你喜欢
    • 2020-08-12
    • 2012-05-04
    • 1970-01-01
    • 1970-01-01
    • 2013-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-24
    相关资源
    最近更新 更多