我尝试使用代码在主布局中添加两个布局 但是在将布局设置为框架并将其添加到视图中时会出现错误
public class MainActivity extends Activity {
FrameLayout rootLayout;
FrameLayout frame1,frame2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rootLayout=new FrameLayout(getApplicationContext());
frame1=(FrameLayout)findViewById(R.layout.frame1);
frame2=(FrameLayout)findViewById(R.layout.frame2);
LayoutParams param1=new FrameLayout.LayoutParams(180,300);
LayoutParams param2=new FrameLayout.LayoutParams(180,300);
param1.gravity=Gravity.LEFT;
param2.gravity=Gravity.RIGHT;
param1.setMargins(0, 0, 30, 30);
param2.setMargins(30, 0, 0, 30);
frame1.setLayoutParams(param1);
frame2.setLayoutParams(param2);
rootLayout.addView(frame1);
rootLayout.addView(frame2);
}
}
答案 0 :(得分:0)
您必须从其父frame1
中删除frame2
和View
个观看次数...
这可能对你有帮助......
FrameLayout rootLayout;
FrameLayout frame1, frame2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
rootLayout = new FrameLayout(this);
frame1 = (FrameLayout) getLayoutInflater().inflate(R.layout.frame1,null);
frame2 = (FrameLayout) getLayoutInflater().inflate(R.layout.frame2, null);
FrameLayout.LayoutParams param1 = new FrameLayout.LayoutParams(180, 300);
FrameLayout.LayoutParams param2 = new FrameLayout.LayoutParams(180, 300);
param1.gravity = Gravity.LEFT;
param2.gravity = Gravity.RIGHT;
param1.setMargins(0, 0, 30, 30);
param2.setMargins(30, 0, 0, 30);
frame1.setLayoutParams(param1);
frame2.setLayoutParams(param2);
rootLayout.addView(frame1);
rootLayout.addView(frame2);
setContentView(rootLayout);
}