-1

我正在创作TableLayout

xml代码TableLayout

<TableLayout
        android:id="@+id/productList"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
</TableLayout>

我想动态添加n行,但我只想像这张图片一样在每行中添加2 列

动态具有 2 列的 TableView

android:strechColumns在布局文件中使用过,但没有成功。

我怎样才能动态解决这个问题。?

请帮忙...

提前致谢。

4

2 回答 2

4

这是activity_table_layout_ex xml:

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

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >

        <HorizontalScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fillViewport="true" >

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <TableLayout
                    android:id="@+id/tableLayoutProduct"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:background="@android:color/white"
                    android:stretchColumns="*" >
                </TableLayout>
            </LinearLayout>
        </HorizontalScrollView>
    </ScrollView>

</LinearLayout>

这是您的活动代码:

import android.app.Activity;
import android.os.Bundle;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class TableLayoutEx extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_table_layout_ex);

         TableLayout tableLayoutProduct = (TableLayout) findViewById(R.id.tableLayoutProduct);

            String[] headingTitle = new String[] { "Col1", "Col2", "Col3", "Col4" };

            TableRow tableRow = new TableRow(this);
            tableRow.setBackgroundColor(getResources().getColor(android.R.color.black));

            for (int i = 0; i < headingTitle.length; i++) {
                TextView textView = new TextView(this);
                textView.setText(headingTitle[i]);
                textView.setTextColor(getResources().getColor(android.R.color.white));
                textView.setPadding(5, 10, 5, 10);
                tableRow.addView(textView);
            }
            tableLayoutProduct.addView(tableRow);
    }

}

试试这个工作代码。您可以通过遵循此示例来实现。

于 2013-10-04T06:09:42.347 回答
1
 <TableLayout 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:stretchColumns="3"
    android:id="@+id/tableLayout"
     >

    <TableRow>
        <LinearLayout 
            android:layout_height="wrap_content"
            android:layout_width="0dp"
            android:weightSum="3">

            <View android:layout_width="0dp" 
                android:layout_height="wrap_content" 
                android:layout_weight="1"
            </View>
            <View android:layout_width="0dp" 
                android:layout_height="wrap_content" 
                android:layout_weight="1"
            </View>
            <View android:layout_width="0dp" 
                android:layout_height="wrap_content" 
                android:layout_weight="1"
            </View>
        </LinearLayout>
     </TableLayout>

 TableLayout tl=(TableLayout)findViewById(R.id.tableLayout);    
 TableRow tr1 = new TableRow(this);
 tr1.setLayoutParams(new LayoutParams( LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
 t1.addView(tr1);
 LinearLayout ll = new LinearLayout(this);
 ll.setOrientation(LinearLayout.HORIZONTAL);
//LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, height, weight);
  LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0dp,LayoutParams.WRAP_CONTENT, 3);
  tr1.addView(ll, params);
  View v1= new View(this);
  View v2= new View(this);
  View v3= new View(this);
  ll.addView(v1, new LayoutParams(0dp, LayoutParams.WRAP_CONTENT,1));
  ll.addView(v2, new LayoutParams(0dp, LayoutParams.WRAP_CONTENT,1));
  ll.addView(v3, new LayoutParams(0dp, LayoutParams.WRAP_CONTENT,1));
于 2013-10-04T06:14:57.183 回答