2011-08-12 62 views
0

我使用xml創建了一個複合控件,並且可以通過放置其他xml 佈局來重新使用它。但我想在運行時使用java代碼添加它。我試着下面的代碼,但只有TextView的是可見的,並且不表示的化合物控制...如何在運行時將複合控件添加到佈局?

我安靜新到Android所以希望得到任何幫助或建議..

複合 - 控制佈局

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/border_lines" 

> 
    <TextView android:id="@+id/msg_title" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="SAMPLE MESSAGE TITLE" 
    /> 
    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
    > 
     <Button android:id="@+id/btn_shw" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:text="SHOW MSG" 
      android:layout_weight="1" 
     /> 
     <Button android:id="@+id/btn_dis" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:text=" DISABLE" 
      android:layout_weight="1" 
     /> 
     <Button android:id="@+id/btn_del" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:text=" DELETE " 
      android:layout_weight="1" 
     /> 
    </LinearLayout>  
</LinearLayout> 

化合物的對照CODE

public class RemainderControl extends LinearLayout 
{ 
    Button btn1,btn2,btn3; 
    TextView tv1; 
    public RemainderControl(Context context) 
    { 
     super(context); 
      LayoutInflater inflater=LayoutInflater.from(context); 
     inflater.inflate(R.layout.remainder_control,this); 

     loadviews(); 
    } 

    public RemainderControl(Context context,AttributeSet attrs) 
    { 
     super(context,attrs); 

     LayoutInflater inflater=LayoutInflater.from(context); 
     inflater.inflate(R.layout.remainder_control,this); 

     loadviews(); 


    } 

    private void loadviews() 
    { 
     btn1=(Button)findViewById(R.id.btn_shw); 
     btn2=(Button)findViewById(R.id.btn_dis); 
     btn3=(Button)findViewById(R.id.btn_del); 
     tv1=(TextView)findViewById(R.id.msg_title); 
      btn1.setOnClickListener(new View.OnClickListener() 
     { 
      @Override 
      public void onClick(View v) 
      { 
       tv1.setText("SHOW BUTTON PRESSED");  
      } 
     }); 
     btn2.setOnClickListener(new View.OnClickListener() 
     { 
      @Override 
      public void onClick(View v) 
      { 
       tv1.setText("DISABLE BUTTON PRESSED");  
      } 
     }); 
     btn3.setOnClickListener(new View.OnClickListener() 
     { 
      @Override 
      public void onClick(View v) 
      { 
       tv1.setText("DELETE BUTTON PRESSED"); 
      } 
     }); 
     tv1.setOnClickListener(new View.OnClickListener() 
     { 
      @Override 
      public void onClick(View v) 
      { 
       tv1.setText(""); 
      } 
     }); 
    } 
} 

代碼中加入控制

public class RemainderList extends Activity 
{ 
    ScrollView sv1; 
    LinearLayout ll1; 
    deepak.android.remainder.RemainderControl rc1; 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 

     int ht=LayoutParams.WRAP_CONTENT; 
     int wt=LayoutParams.FILL_PARENT; 

     sv1=new ScrollView(this); 
     ll1=new LinearLayout(this); 
     ll1.setOrientation(LinearLayout.VERTICAL); 
     sv1.addView(ll1); 

     TextView tv1=new TextView(this); 
     tv1.setText("THIS IS SAMPLE TEXT"); 
     ll1.addView(tv1,new LinearLayout.LayoutParams(wt,ht)); 

     rc1=new deepak.android.remainder.RemainderControl(this); 
     ll1.addView(rc1,new LinearLayout.LayoutParams(wt,ht)); 

     setContentView(sv1); 
    } 
} 

回答

3

當你的控件包含在XML,public RemainderControl(Context context,AttributeSet attrs)構造函數被調用。但是在你的代碼中你直接調用構造函數public RemainderControl(Context context)。移動所有擴展布局的代碼,並將偵聽器設置爲某種方法(例如init()),並在兩個構造函數中調用它。

+0

當我把「LayoutInflater ...」代碼放在一個單獨的方法中時,我顯示一個錯誤,因爲'context'在方法中不可見。所以我將代碼複製到兩個構造函數,現在它工作正常。但我做錯了什麼。我應該能夠使它與複製工作無關。我對上面的代碼進行了更改,這使得它現在可以正常工作。 – Deepak

+0

當然你可以做它沒有重複。只需將context作爲參數傳遞給'init'方法即可。就像'private void init(Context context){//做些事情}'並且在構造函數中調用它,比如'init(context);' – ernazm

+0

好吧,我明白了,非常感謝 – Deepak

相關問題