2011-08-15 137 views
0

我已经将我的setContentView设置为一个视图(代码),但现在我需要一个xml来添加一个按钮。是否有可能将UI与代码和xml结合起来?如果可能比我应该做什么,并添加我的代码?代码和xml在setcontentview android

下面的代码:

public class Diagram extends Activity { 
/** Called when the activity is first created. */ 

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

     mgrafik = new grafik(this); 

     setContentView(mgrafik); 
    } 
} 

感谢的对你有所帮助。

回答

1

Alhamdulillah,最后我可以结合按钮,创艺,使用RelativeLayout的通过编码。这是源代码。

public class Diagram extends Activity { 

/** Called when the activity is first created. */ 
grafik mgrafik; 

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

    RelativeLayout rel = new RelativeLayout (this); 
    rel.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 

    mgrafik = new grafik(this); 
    RelativeLayout.LayoutParams grf = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
    grf.addRule(RelativeLayout.CENTER_HORIZONTAL); 

    Button btn = new Button(this); 
    btn.setText("Kembali"); 
    RelativeLayout.LayoutParams pbtn = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); 
    pbtn.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 


    rel.addView(mgrafik, grf); 
    rel.addView(btn, pbtn); 

    setContentView(rel); 

} 

}

感谢的一切对你有所帮助。

0

假设grafik是某种View你可能只是把它放到你的XML:<com.whatever.grafik.../>

+0

我试过它在绝对平铺它没有错误。但是当我在android模拟器上运行该程序时,它表示该活动意外停止并需要关闭。任何其他想法? – abdurrahman

+0

可能因为您没有添加第二个构造函数。什么是堆栈跟踪? – mibollma

+0

我应该如何把第二个构造函数?什么是堆栈跟踪?是这[2011-08-16 08:05:43 - CobaPass]安装CobaPass.apk ... [2011-08-16 08:06:25 - CobaPass]成功! [2011-08-16 08:06:25 - CobaPass]开始活动设备模拟器上的coba.pass.CobaPassActivity -5554 [2011-08-16 08:06:27 - CobaPass] ActivityManager:开始:意图{act = android.intent.action.MAIN cat = [android.intent.category.LAUNCHER] cmp = coba.pass/.CobaPassActivity} – abdurrahman