849
技術社區[雲棲]
android Framelayout動態添加組件
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FrameLayout layout= new FrameLayout(this);//定義框架布局器
FrameLayout.LayoutParams params=new FrameLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT);//定義框架布局器參數
FrameLayout.LayoutParams tparams=new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);//定義顯示組件參數
TextView txtview = new TextView(this);//定義組件
txtview.setText("歡迎使用框架布局");
EditText et = new EditText(this);
et.setText("請選擇你喜歡的布局");
Button but=new Button(this);
but.setText("按我");
layout.addView(txtview, tparams);//添加組件
layout.addView(et, tparams);
layout.addView(but, tparams);
setContentView(layout,params);//向屏幕上添加布局顯示器
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
最後更新:2017-04-03 14:54:00