0

我有一个方法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();
4

0 回答 0