每次点击按钮时,我都会在textview
中动态添加linearLayout
的方法,在这种情况下效果很好,但在其他一些情况下,我想在不点击的情况下以编程方式调用它在一个按钮上但它不起作用。但是当我再次点击该按钮时,会出现textviews
。
顺便说一下,这个方法包含在一个片段中。
这是我的代码:
private void add_part() {
int width = bar.getWidth();
int child_count = ((ViewGroup) bar).getChildCount();
if (child_count == 0) {
minus_btn.setEnabled(true);
LayoutParams lparams = new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT);
TextView part = new TextView(getActivity());
part.setLayoutParams(lparams);
part.setGravity(Gravity.CENTER);
bar.addView(part);
} else if (child_count == 1) {
minus_btn.setEnabled(true);
View nextChild = ((ViewGroup) bar).getChildAt(0);
LayoutParams newparams = new LayoutParams(width / 2,
LayoutParams.FILL_PARENT);
nextChild.setLayoutParams(newparams);
LayoutParams lparams = new LayoutParams(width / 2,
LayoutParams.FILL_PARENT);
TextView part = new TextView(getActivity());
part.setLayoutParams(lparams);
bar.addView(part);
} else if (child_count > 1 && child_count < 10) {
minus_btn.setEnabled(true);
for (int i = 0; i < child_count; ++i) {
View nextChild = ((ViewGroup) bar).getChildAt(i);
LayoutParams newparams = new LayoutParams(width
/ (child_count + 1), LayoutParams.FILL_PARENT);
nextChild.setLayoutParams(newparams);
}
LayoutParams lparams = new LayoutParams(width / (child_count + 1),
LayoutParams.FILL_PARENT);
TextView part = new TextView(getActivity());
part.setLayoutParams(lparams);
bar.addView(part);
}
}
我用这种方式称呼它:
add_part();
或
plus_btn.performClick();