Android TableLayout:以编程方式拉伸列

时间:2013-10-04 05:54:05

标签: android android-tablelayout

我正在创建TableLayout

xml代码TableLayout

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

我想动态添加 n no行,但我想在每行中只添加 2列 喜欢这个图像

TableView with 2 columns dynamically

我在布局文件中使用过android:strechColumns但未成功。

我如何动态地解决这个问题。?

请帮忙......

提前致谢。

2 个答案:

答案 0 :(得分:3)

这是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);
    }

}

试试这个有效的代码。您可以通过以下示例来实现这一目标。

答案 1 :(得分: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));