0
Javaクラスのソース(DynamicTableLayout.java)を使用して動的なTableLayoutを作成しました。このコードを使用してdymanic tablelayoutをlinearlayoutのxmlフォームに渡すにはどうすればいいですか?
public class DynamicTableLayout extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
TableLayout layout = new TableLayout (this);
layout.setLayoutParams(new TableLayout.LayoutParams(4,3));
TableLayout.LayoutParams rowLp = new TableLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT,
1.0f);
rowLp.setMargins(6, 0, 0,0);
TableRow.LayoutParams cellLp = new TableRow.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT,
1.0f);
cellLp.setMargins(6,0, 0, 0);
layout.setPadding(3,1,1,1);
for (int f=0; f<=6; f++) {
TableRow tr = new TableRow(this);
tr.setBackgroundColor(Color.BLACK);
tr.setPadding(1,1,1,1);
TableRow.LayoutParams llp = new TableRow.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
llp.setMargins(2, 0, 0, 0);
for (int c=0; c<=3; c++) {
LinearLayout cell = new LinearLayout(this);
cell.setBackgroundColor(Color.WHITE);
cell.setLayoutParams(llp);//2px border on the right for the cell
TextView b = new TextView (this);
b.setText("Sample");
b.setTextSize(10.0f);
b.setPadding(0, 0, 0, 0);
cell.addView(b);
tr.addView(cell,cellLp);
}
layout.addView(tr,rowLp);
}
super.setContentView(layout);
} }
main.xmlコードがある
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/linear1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/bottom_black_pane"
android:orientation="horizontal">
<TextView
android:id="@+id/textview1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Color range"
android:gravity="center"
android:textSize="20dp"
android:textColor="#ffffff"
android:paddingTop="10dp"/>
</LinearLayout>
<LinearLayout
android:id="@+id/linear2"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView android:id="@+id/imageView1"
android:src="@drawable/palette"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageView>
</LinearLayout>
<LinearLayout
android:id="@+id/linear2"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</LinearLayout>
<LinearLayout
android:id="@+id/linear3"
android:layout_height="wrap_content"
android:background="@drawable/bottom_black_pane"
android:layout_width="wrap_content">
<LinearLayout
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1" android:layout_width="355dp">
<Button
android:id="@+id/Btn1"
android:layout_width="30dp"
android:text="All"
android:paddingTop="10dp"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:background="#000000"
android:layout_weight="0.18"/>
<Button android:layout_width="30dp"
android:layout_weight="0.18"
android:textColor="#ffffff"
android:background="#000000"
android:paddingTop="10dp"
android:id="@+id/Btn2"
android:layout_height="wrap_content"
android:text="Virbate"></Button>
<Button
android:id="@+id/Btn2"
android:layout_width="30dp"
android:text="Warm"
android:textColor="#ffffff"
android:paddingTop="10dp"
android:background="#000000"
android:layout_height="wrap_content"
android:layout_weight="0.18"/>
<Button
android:id="@+id/Btn2"
android:layout_width="40dp"
android:text="Fresh"
android:textColor="#ffffff"
android:paddingTop="10dp"
android:background="#000000"
android:layout_height="wrap_content"
android:layout_weight="0.18"/>
<Button
android:id="@+id/Btn2"
android:layout_width="40dp"
android:text="Calm"
android:textColor="#ffffff"
android:paddingTop="10dp"
android:background="#000000"
android:layout_height="wrap_content"
android:layout_weight="0.09"/>
</LinearLayout>
</LinearLayout>
<ScrollView
android:id="@+id/ScrollView01"
android:layout_width="fill_parent"
android:scrollbars="vertical"
android:layout_height="fill_parent">
<HorizontalScrollView android:id="@+id/horizon"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout android:id="@+id/list_parent"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:weightSum="1">
</LinearLayout>
</HorizontalScrollView>
</ScrollView>
</LinearLayout>
メインクラスのコードID
public class DuluxColorGridActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
私は一緒に両方のレイアウトを統合するために何をするか、Iフィールドのmain.xmlレイアウトに動的レイアウトを統合したいlinearLayout id = list_parent below私は宣言します。この問題を解決するのに役立ちます。あなたがXMLで
をクラス(packagename+ classnaem)
の絶対パスを提供する必要が任意のXMLにカスタムビューを追加するには
<LinearLayout android:id="@+id/list_parent"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:weightSum="1">
</LinearLayout>
Sunil Kumar Sahooさん、ありがとうございます。このレイアウトでは、列の線形レイアウトは追加されていません。線形レイアウトのコンストラクタを作成するとエラーが表示されます。どうすればこの問題を解決できますか?私を助けてください –