2016-12-02 71 views
1

這是我的代碼爲一個n應用程序,我想在一行中添加文字瀏覽 動態按一下按鈕,而不是工作你能告訴我什麼是錯的代碼,我只是一個初學者。 感謝您的期待。行發電機與textviews

public class MainActivity extends AppCompatActivity { 

    Button save; 
    TableLayout tableLayout; 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Button save = (Button) findViewById(R.id.save); 

     TableLayout tableLayout = (TableLayout)   findViewById(R.id.table1); 

     save.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       TableRow tableRow; 
       TextView textView; 
       TableLayout tableLayout = (TableLayout)  findViewById(R.id.table1); 

       for (int i = 0; i < 4; i++) { 
        tableRow = new TableRow(getApplicationContext()); 
        for (int j = 0; j < 3; j++) { 
         textView = new  TextView(getApplicationContext()); 
         textView.setText("@@@@"); 
         textView.setPadding(20, 20, 20, 20); 
         tableRow.addView(textView); 
        } 


        tableLayout.addView(tableRow); 
       }}}); 


       setContentView(tableLayout); 






    } 
} 

and my xml layout 

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.kalla.monu.saplist.MainActivity"> 


    <TableLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentEnd="true" 
     android:layout_marginEnd="28dp" 
     android:layout_marginTop="160dp" 
     android:id="@+id/table1" 
     android:background="@color/colorPrimaryDark"> 

     <TableRow 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="@android:color/darker_gray"/> 


      <TableRow 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:backgroundTint="@android:color/holo_green_dark"  /> 

     <TableRow 
       android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 

     <TableRow 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 
    </TableLayout> 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 

     android:text="save" 
     android:id="@+id/save" /> 
    </RelativeLayout> 

回答

0

試試這個,

我假設你正試圖添加兩個動態tablerows和textviews。所以我已經從佈局xml中刪除了現有的表格。

TableLayout tableLayout = (TableLayout) findViewById(R.id.table1); 

save.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View view) { 
    TableRow tableRow; 
    TextView textView; 
    for (int i = 0; i < 4; i++) { 
    tableRow = new TableRow(this); 
    for (int j = 0; j < 3; j++) { 
     textView = new TextView(this); 
     textView.setText("@@@@"); 
     textView.setPadding(20, 20, 20, 20); 
     tableRow.addView(textView); 
    } 
    table1.addView(tableRow); 
}}}); 

在XML:

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