Androidダイナミック追加ボタン

4308 ワード

      LayouInflater :

   layout xml         View   。         ,     getLayoutInflater()    getSystemService(String)       LayoutInflater  ,       context    ,            。

  :

[java] view plaincopy
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  


public View inflate (int resource, ViewGroup root)

Since: API Level 1
Inflate a new view hierarchy from the specified xml resource. Throws InflateException if there is an error.
Parameters

resource     ID for an XML layout resource to load (e.g., R.layout.main_page) 
root     Optional view to be the parent of the generated hierarchy.
Returns

The root View of the inflated hierarchy. If root was supplied, this is the root View; otherwise it is the root of the inflated XML file.


        xml  (guess_button.xml)      buttonTableLayout:

[html] view plaincopy
<?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"  
    android:orientation="vertical" >  
  
    <Button  
        android:id="@+id/newGuessButton"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_weight="1" />  
</LinearLayout>   
(guess_button.xml)


[html] view plaincopy
<TableLayout  
       android:id="@+id/buttonTableLayout"  
       android:layout_width="match_parent"  
       android:layout_height="wrap_content"  
       android:layout_weight="1"  
       android:stretchColumns="0,1,2" >  
  
  
       <TableRow  
           android:id="@+id/tableRow0"  
           android:layout_width="match_parent"  
           android:layout_height="wrap_content"   
           android:orientation="horizontal">  
  
       </TableRow>  
  
  
       <TableRow  
           android:id="@+id/tableRow1"  
           android:layout_width="match_parent"  
           android:layout_height="wrap_content"   
           android:orientation="horizontal">  
  
       </TableRow>  
  
  
       <TableRow  
           android:id="@+id/tableRow2"  
           android:layout_width="match_parent"  
           android:layout_height="wrap_content"   
           android:orientation="horizontal">  
  
       </TableRow>  
   </TableLayout>  
(main.xml          buttonTableLayout,            buttonTableLayout,  buttonTableLayout =(TableLayout)  findViewById(R.id.buttonLayout);)

             :

[java] view plaincopy
LayoutInflater inflater = (LayoutInflater)context.getSystemService (Context.LAYOUT_INFLATER_SERVICE);  
Button newGuessButton =(Button)inflater.inflater(R.layout.guess_button,null); //      
newGuessButton.setText("abc"); //        
newGuessButton.setOnClickListener(guessButtonListener); //  Listener  
(TableRow) buttonTableLayout.getChildAt(1);  // newGuessButton  table     

OK