/ / Android TableLayout:列をプログラムでストレッチ-android、android-tablelayout

Android TableLayout:列をプログラムで伸ばす - android、android-tablelayout

私は作成しています 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 レイアウトファイルにありますが、成功しません。

この問題を動的に解決するにはどうすればよいですか?

助けてください...

前もって感謝します。

回答:

回答№1は2

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

}

この作業コードを試してください。この例を実行することでそれを達成できます。


回答№2の場合は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));