2011-08-12 61 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

相关问题