2016-12-02 7 views
1

これは、行のtextviewsを追加したい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から既存のtablerowsを削除しました。 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); 
}}}); 

<TableLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/table1"/> 
関連する問題