动态生成TableRow

时间:2016-03-29 09:12:43

标签: android tablelayout tablerow

我正在动态生成table_row但是视图没有显示在table_layout.i中,我的代码中没有fig_out错误。每次用户按下按钮table_row将自动生成。 这是我的代码......

submitButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        getSerialNumber = serialNumber.getText().toString();
        getItems = items.getText().toString();
        getBrand = brand.getText().toString();
        getPrice = price.getText().toString();

        TableRow row = new TableRow(StoreFirstActivity.this);
        LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        row.setLayoutParams(layoutParams);

        TextView t = new TextView(StoreFirstActivity.this);
        t.setLayoutParams(layoutParams);
        t.setText(getSerialNumber.toString());

        TextView t1 = new TextView(StoreFirstActivity.this);
        t1.setLayoutParams(layoutParams);
        t1.setText(getItems.toString());

        TextView t2 = new TextView(StoreFirstActivity.this);
        t2.setLayoutParams(layoutParams);
        t2.setText(getBrand.toString());

        TextView t3 = new TextView(StoreFirstActivity.this);
        t3.setLayoutParams(layoutParams);
        t3.setText(getPrice.toString());


        row.addView(t);
        row.addView(t1);
        row.addView(t2);
        row.addView(t3);

        Log.i(TAG, getSerialNumber.toString());
        Log.i(TAG, getPrice.toString());
        Log.i(TAG, getItems.toString());

        tableLayout.addView(row);

        serialNumber.setText("");
        items.setText("");
        brand.setText("");
        price.setText("");

        Toast.makeText(StoreFirstActivity.this, "Items Submitted", Toast.LENGTH_SHORT).show();

    }
});

这是xml。

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:context="com.example.hamza.store.StoreFirstActivity"
        android:orientation="vertical"
        tools:showIn="@layout/activity_store_first">

        <ScrollView
       android:layout_width="match_parent"
       android:layout_height="200dp"
       android:layout_marginTop="15dp"
       android:id="@+id/scrollView">

       <TableLayout
           android:layout_height="match_parent"
           android:layout_width="match_parent"
           android:id="@+id/tableLayout">
           <TableRow
               android:id="@+id/one">

               <TextView
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:text="Y01398"
                   android:layout_marginLeft="10dp"
                   android:layout_column="0"
                   android:textColor="#808080"
                   android:id="@+id/textView7" />

               <TextView
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:text="Cigrate"
                   android:layout_column="1"
                   android:layout_marginLeft="45dp"
                   android:textColor="#808080"
                   android:id="@+id/textView8" />

               <TextView
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:text="Camel"
                   android:layout_marginLeft="52dp"
                   android:textColor="#808080"
                   android:id="@+id/textView9" />

               <TextView
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:text="5$"
                   android:layout_marginLeft="55dp"
                   android:textColor="#808080"
                   android:id="@+id/textView10" />
           </TableRow>




       </TableLayout>
   </ScrollView>
    </LinearLayout>

这就是我想在table_lyout中显示table_row的方法 enter image description here

2 个答案:

答案 0 :(得分:0)

试试这个

    TableRow row = new TableRow(StoreFirstActivity.this);
    LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    row.setLayoutParams(layoutParams);
    tableLayout.addView(row);

    LinearLayout ll = new LinearLayout(StoreFirstActivity.this);
    LinearLayout.LayoutParams llParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 
            LayoutParams.WRAP_CONTENT);
    ll.setOrientation(LinearLayout.VERTICAL);
    ll.setLayoutParams(llParams);

    TextView t = new TextView(StoreFirstActivity.this);
    t.setLayoutParams(llParams);
    t.setText(getSerialNumber.toString());

    TextView t1 = new TextView(StoreFirstActivity.this);
    t1.setLayoutParams(llParams);
    t1.setText(getItems.toString());

    TextView t2 = new TextView(StoreFirstActivity.this);
    t2.setLayoutParams(llParams);
    t2.setText(getBrand.toString());

    TextView t3 = new TextView(StoreFirstActivity.this);
    t3.setLayoutParams(llParams);
    t3.setText(getPrice.toString());

    ll.addView(t);
    ll.addView(t1);
    ll.addView(t2);
    ll.addView(t3);

    row.addView(ll);

答案 1 :(得分:0)

它有

  

import android.view.ViewGroup.LayoutParams

什么时候应该

  

导入android.widget.TableRow.LayoutParams

现在一切正常。