我已经定制了扩展ViewGroup
类的类。在它里面,我想添加一些视图,包括两个必须相邻的按钮。我想出了使用这个视图创建XML文件的想法,然后使用addView添加它。不幸的是它不能解决问题。将LinearLayout添加到ViewGroup
我认为更好的第二个想法是创建LineraLayout以及两个按钮,设置所有设置然后添加。
这是代码:
//adding view to view group
addView(createView(ctx));
//function creating linearlayout and buttons
private View createView(Context ctx){
LinearLayout l = new LinearLayout(ctx);
l.setOrientation(LinearLayout.HORIZONTAL);
l.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
Button btnL = new Button(ctx);
btnL.setText("Text1");
Button btnR = new Button(ctx);
btnR.setText("Text2");
l.addView(btnL);
l.addView(btnR);
return l;
}
的问题是,我现在不看这个观点的。我的意思是从LinearLayout创建的。 LogCat中没有错误。
有人可以告诉我我要做什么来添加它吗?
编辑:
这是onLayout
代码,我没有onMeasure
:
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int w = getWidth();
int h = getHeight();
for (int i = 0; i < NUM_CHILDREN; i++) {
View v = getChildAt(i);
int ii = i * 4;
float fl = w * COORDS[ii]/GRID_WIDTH;
float ft = h * COORDS[ii + 1]/GRID_HEIGHT;
float fr = fl + w * COORDS[ii + 2]/GRID_WIDTH;
float fb = ft + h * COORDS[ii + 3]/GRID_HEIGHT;
v.layout(Math.round(fl), Math.round(ft), Math.round(fr),
Math.round(fb));
}
}
总的来说,我只是用巨大的表,其中我有什么样的信息给出确切尺寸应该是视角。
这将有助于看到代码为您定制'ViewGroup'的'onMeasure'和'onLayout'方法。 – Luksprog
@Luksprog刚刚添加。 – sebap123
为什么你首先从xml加载两个按钮的想法不起作用? –