2011-12-30 24 views
0

我使用java類中的源代碼(DynamicTableLayout.java)創建了一個動態TableLayout。使用此代碼如何在linearlayout中將dymanic tablelayout傳遞給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); 




} 

}

我該怎麼辦的兩種佈局整合在一起,我想要將動態佈局整合到我聲明的字段linearLayout id = list_parent的main.xml佈局中。幫助我解決這個問題。

<LinearLayout android:id="@+id/list_parent" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent" 
    android:orientation="horizontal" 
    android:weightSum="1"> 


</LinearLayout> 

回答

0

要自定義視圖添加到任何XML,你需要提供XML (packagename+ classnaem)類的絕對路徑

看到Custom Composite Android Component

實例創建自定義tablelayout

import android.content.Context; 
import android.util.AttributeSet; 
import android.widget.TableLayout; 

public class CustomTableLayout extends TableLayout{ 

    public CustomTableLayout(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     // Write your own code here 
    } 

} 

要在xml中添加abive視圖,您可以使用下面的代碼

<com.sunil.list.item.CustomTableLayout 
     android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     > 
+0

謝謝Sunil Kumar Sahoo,但在此佈局中未添加column的linearlayout,它顯示爲linearlayout創建構造函數的錯誤。我怎麼解決這個問題。請幫幫我 – 2012-01-02 07:40:21

相關問題