2012-06-09 58 views
1

我想創建動態tablelayout。我已經得到了這些節點(beause每個節點有獨立的風格):如何創建具有未知行數和列數的動態tablelayout?

  • 標題行
  • 頭細胞
  • 奇數行
  • 奇數行細胞
  • 偶數排
  • 連排cell 是這樣的:

    在這裏輸入代碼

    <TableRow 
        android:id="@+id/header_row" 
        style="@style/HeaderRow" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" > 
    
        <TextView 
         android:id="@+id/header_tv" 
         style="@style/HeaderText" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" /> 
    </TableRow> 
    
    <TableRow 
        android:id="@+id/bodyrow_odd" 
        style="@style/BodyRowOdd" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" > 
    
        <TextView 
         android:id="@+id/body_tv_odd" 
         style="@style/BodyTextOdd" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" /> 
    </TableRow> 
    
    <TableRow 
        android:id="@+id/bodyrow_even" 
        style="@style/BodyRowEven" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" > 
    
        <TextView 
         android:id="@+id/body_tv_even" 
         style="@style/BodyTextEven" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" /> 
    </TableRow> 
    

,如果我想通過虛報每個節點創建動態界面,我必須有每個節點單獨的XML?'在這裏輸入代碼

+0

http://www.warriorpoint.com/blog/2009/07/01/android-creating-tablerow-rows-inside-a-tablelayout-programatically/ – Bhavin

+0

@Bhavin我真的想在styles.xml中有樣式不是* .java。 – xliiv

回答

-1

MainActivity.class

import android.graphics.Color; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.Gravity; 
import android.widget.TableLayout; 
import android.widget.TableRow; 
import android.widget.TextView; 

public class MainActivity extends AppCompatActivity { 

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

public void init() { 
TableLayout stk = (TableLayout) findViewById(R.id.table_main); 
TableRow tbrow0 = new TableRow(this); 
TextView tv0 = new TextView(this); 
tv0.setText(" Sl.No "); 
tv0.setTextColor(Color.WHITE); 
tbrow0.addView(tv0); 
TextView tv1 = new TextView(this); 
tv1.setText(" Product "); 
tv1.setTextColor(Color.WHITE); 
tbrow0.addView(tv1); 
TextView tv2 = new TextView(this); 
tv2.setText(" Unit Price "); 
tv2.setTextColor(Color.WHITE); 
tbrow0.addView(tv2); 
TextView tv3 = new TextView(this); 
tv3.setText(" Stock Remaining "); 
tv3.setTextColor(Color.WHITE); 
tbrow0.addView(tv3); 
// Apply here your HeaderRow style. 
tbrow0.setBackgroundColor(Color.BLUE); 
stk.addView(tbrow0); 
for (int i = 0; i < 25; i++) { 
TableRow tbrow = new TableRow(this); 
TextView t1v = new TextView(this); 
t1v.setText("" + i); 
t1v.setTextColor(Color.WHITE); 
t1v.setGravity(Gravity.CENTER); 
tbrow.addView(t1v); 
TextView t2v = new TextView(this); 
t2v.setText("Product " + i); 
t2v.setTextColor(Color.WHITE); 
t2v.setGravity(Gravity.CENTER); 
tbrow.addView(t2v); 
TextView t3v = new TextView(this); 
t3v.setText("Rs." + i); 
t3v.setTextColor(Color.WHITE); 
t3v.setGravity(Gravity.CENTER); 
tbrow.addView(t3v); 
TextView t4v = new TextView(this); 
t4v.setText("" + i * 15/32 * 10); 
t4v.setTextColor(Color.WHITE); 
t4v.setGravity(Gravity.CENTER); 
tbrow.addView(t4v); 
if (i % 2 == 0) { 
// Apply here your BodyRowEven style. 
tbrow.setBackgroundColor(Color.GRAY); 
} else { 
// Apply here your BodyRowOdd style. 
tbrow.setBackgroundColor(Color.LTGRAY); 
} 
stk.addView(tbrow); 
} 
} 
} 

activity_main.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"> 

<ScrollView 
android:id="@+id/scrollView1" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:layout_alignParentLeft="true" 
android:background="#ffffff"> 

<HorizontalScrollView 
android:id="@+id/hscrll1" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content"> 

<RelativeLayout 
android:id="@+id/RelativeLayout1" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:layout_gravity="center" 
android:orientation="vertical"> 

<TableLayout 
android:id="@+id/table_main" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_centerHorizontal="true" /> 
</RelativeLayout> 
</HorizontalScrollView> 
</ScrollView> 
</LinearLayout> 
相關問題