1

基本上我正在尝试向表中添加行,我需要以编程方式执行此操作。

我可以让它添加我想要的内容,但不完全是我想要的。

例如,我希望左侧有一个 textview,右侧有一个按钮 edittext 按钮。

基本上我想要它如下图所示:

.

另外我不想只是和宽度到文本视图。

无论如何,这是我到目前为止所做的:

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:padding="5dp" xmlns:android="http://schemas.android.com/apk/res/android">

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <Button
                android:id="@+id/btnOutstandingJob"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Outstanding Job" />

            <Button
                android:id="@+id/btnVanStock"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Van Stock" />

            <Button
                android:id="@+id/btnRegister"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:text="Register User" />
        </LinearLayout>

    </ScrollView>

    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <Button
            android:id="@+id/btnLogout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginTop="5dp"
            android:text="Log Out" />
    </RelativeLayout>

</LinearLayout>

爪哇:

TableLayout table = (TableLayout) findViewById(R.id.tableLayout1);

TableRow row = new TableRow(vanstock.this);

RelativeLayout RowLayout = new RelativeLayout(vanstock.this);
RowLayout.setId(99);

TextView lblItem = new TextView(vanstock.this);
lblItem.setId(1);
lblItem.setText("ddfsgsdfgs");

RelativeLayout.LayoutParams lblitemParam = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT,
        RelativeLayout.LayoutParams.WRAP_CONTENT);
lblitemParam.addRule(RelativeLayout.CENTER_VERTICAL,
        RelativeLayout.TRUE);
lblitemParam.addRule(RelativeLayout.ALIGN_PARENT_LEFT,
        RelativeLayout.TRUE);

lblItem.setLayoutParams(lblitemParam);
RowLayout.addView(lblItem);

Button btnPlus = new Button(vanstock.this);
btnPlus.setId(4);
btnPlus.setText("+");
btnPlus.setWidth(40);

RelativeLayout.LayoutParams btnPlusParams = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT,
        RelativeLayout.LayoutParams.WRAP_CONTENT);
btnPlusParams.addRule(RelativeLayout.ALIGN_PARENT_TOP,
        RelativeLayout.TRUE);
btnPlusParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,
        RelativeLayout.TRUE);
btnPlus.setLayoutParams(btnPlusParams);

RowLayout.addView(btnPlus);

EditText txtItem = new EditText(vanstock.this);
txtItem.setId(3);
txtItem.setWidth(40);

RelativeLayout.LayoutParams txtItemParams = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT,
        RelativeLayout.LayoutParams.WRAP_CONTENT);
txtItemParams.addRule(RelativeLayout.ALIGN_PARENT_TOP,
        RelativeLayout.TRUE);
txtItemParams.addRule(RelativeLayout.LEFT_OF, btnPlus.getId());
txtItem.setLayoutParams(txtItemParams);

RowLayout.addView(txtItem);

Button btnMinus = new Button(vanstock.this);
btnMinus.setId(2);
btnMinus.setText("-");
btnMinus.setWidth(40);

RelativeLayout.LayoutParams btnMinusParams = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT,
        RelativeLayout.LayoutParams.WRAP_CONTENT);
btnMinusParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
btnMinusParams.addRule(RelativeLayout.LEFT_OF, txtItem.getId());
btnPlus.setLayoutParams(btnMinusParams);

RowLayout.addView(btnPlus);
row.addView(RowLayout);

table.addView(row, new LayoutParams(LayoutParams.WRAP_CONTENT,
        LayoutParams.WRAP_CONTENT));

我已经尝试过搜索等,但我仍然无法让它正常工作。任何帮助将不胜感激!

4

1 回答 1

2
 LayoutInflater inflater = getLayoutInflater();
 TableRow row = (TableRow) inflater.inflate(R.layout.table,
                _tablelayout, false);
 TextView textview = (TextView) row.findViewById(R.id.rowdata);

像这样你可以满足你的要求。这就足够了吗?

最佳 Vk

于 2012-02-06T14:10:08.903 回答