有一個自定義的相對佈局,並增加了一個按鈕,它我如何相對佈局添加到瀏覽
RelativeLayout rel_layout = new RelativeLayout(mcontext);
RelativeLayout.LayoutParams rel_param = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
rel.setLayoutParams(rel_param);
Button b = new Button(mcontext);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
b.setLayoutParams(params);
b.setText("Test");
rel_layout.addView(b);
現在,我想這個相對佈局被添加到一個視圖。我的視圖類看起來這是
public class CustomView extends View {
Context mcontext;
public CustomView(Context context) {
super(context);
this.mcontext = context;
}
}
,並在主要活動,我在我的setConentView()
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new CustomView(this));
}
}
調用這個觀點所以現在在屏幕我應該有相應的佈局,其中的按鈕。我不應該使用任何XMl。
我需要在如何創建要添加到我的CustomView類
希望我已經解釋清楚我的問題的動態相對增加的幫助。
添加自定義視圖相對佈局 – Raghunandan
我尋找其他方式輪..我想在CustomView類添加相對佈局 –