线性布局以编程方式添加滚动视图

Linear Layout add scrollView in programmatically

一切正常,但我无法添加 Scrollview,因为我在 scrollView.addView(mainlinearLayout);

中遇到错误

有类似的问题,但我没有找到答案。如果你能帮助我,我会很高兴。谢谢

这是错误

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

这是activity_main.xml的代码

  <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/LinLay"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

    </LinearLayout>

这是 MainActivity 的代码

public void drawer() {
        String[] word=s.split(" ");
        scrollView=new ScrollView(this);
        mainlinearLayout = (LinearLayout) findViewById(R.id.LinLay);
        mainlinearLayout.setVerticalScrollBarEnabled(true);
        scrollView.addView(mainlinearLayout);
        childLinearLayout = getLinearLayout();


        for (int i = 1; i < word.length; i++) {
            final TextView textView = new TextView(this);
            textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            textView.setTextSize(17);
            textView.setPadding(5, 5, 5, 5);
            textView.setText(word[i-1].toString());
            childLinearLayout.addView(textView);

            if (i % separatorNum == 0 && i != 0) {
                mainlinearLayout.addView(childLinearLayout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                setContentView(mainlinearLayout);
                childLinearLayout=getLinearLayout();
            }

            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(getApplicationContext(), textView.getText().toString(), Toast.LENGTH_LONG).show();
                }
            });
        }
     }

如果您 activity_main.xml 作为 Activity 的内容视图:

setContentView(R.layout.activity_main)

然后您将视图附加到 activity,因此给它(以及 id=LinLay 的 LinearLayout)一个父对象。

如果您想让 ScrollView 成为 Activity 中的主要 UI 元素,那么您应该这样做

setContentView(scrollView)

或者更好的是在 XML 中做到这一点。一旦 ScrollView 成为父级,您就可以添加您的 LinearLayout。

您正在布局中添加滚动视图。

如果您在滚动视图中添加布局,那么永远记住您只能在滚动视图中添加一个布局。您也可以在滚动视图中添加线性布局,然后可以在同一视图中添加多个布局。

试试下面的代码,它会检查子项是否有父项,如果有则删除视图,这将对您有所帮助。

 public void drawer() {
                String[] word=s.split(" ");
                scrollView=new ScrollView(this);
                mainlinearLayout = (LinearLayout) findViewById(R.id.LinLay);
                mainlinearLayout.setVerticalScrollBarEnabled(true);
                scrollView.addView(mainlinearLayout);
                childLinearLayout = getLinearLayout();


                for (int i = 1; i < word.length; i++) {
                    final TextView textView = new TextView(this);
                    textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                    textView.setTextSize(17);
                    textView.setPadding(5, 5, 5, 5);
                    textView.setText(word[i-1].toString());
    if (textView.getParent() != null) {
                            ((ViewGroup) textView.getParent()).removeView(textView);
                        }
                    childLinearLayout.addView(textView);

                    if (i % separatorNum == 0 && i != 0) {
                        mainlinearLayout.addView(childLinearLayout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                        setContentView(mainlinearLayout);
                        childLinearLayout=getLinearLayout();
                    }

                    textView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Toast.makeText(getApplicationContext(), textView.getText().toString(), Toast.LENGTH_LONG).show();
                        }
                    });
                }

            }

你可以试试这个,在调用这个之前尝试设置setContentView(R.layout.activity_main)

问题是您的 LinearLayout 已经附加到 activity 查看与您尝试添加到 scrollview 的相同布局,因此出现错误。

public void drawer() {
    //String s = "sdfsdfsdf sdfsdfsd sdfsdf sdfs sdf sdf sdf sdfdsfsdf sfsdf ssdfdsf sdfsd";
    int separatorNum = 5;
    String[] word = s.split(" ");
    scrollView = new ScrollView(this);
    mainlinearLayout = (LinearLayout) findViewById(R.id.LinLay);
    mainlinearLayout.setVerticalScrollBarEnabled(true);

    mainlinearLayout.addView(scrollView);

    LinearLayout linearLayout = new LinearLayout(this);
    linearLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    linearLayout.setOrientation(LinearLayout.VERTICAL);
    scrollView.addView(linearLayout);

    LinearLayout childLinearLayout = getLinearLayout();


    for (int i = 1; i < word.length; i++) {
        final TextView textView = new TextView(this);
        textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        textView.setTextSize(17);
        textView.setPadding(5, 5, 5, 5);
        textView.setText(word[i - 1].toString());
        childLinearLayout.addView(textView);

        if (i % separatorNum == 0 && i != 0) {
            linearLayout.addView(childLinearLayout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
           // setContentView(mainlinearLayout);
            childLinearLayout = getLinearLayout();
        }

        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getApplicationContext(), textView.getText().toString(), Toast.LENGTH_LONG).show();
            }
        });
    }

}

private LinearLayout getLinearLayout() {
    LinearLayout childLinearLayout = new LinearLayout(this);
    childLinearLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    childLinearLayout.setOrientation(LinearLayout.HORIZONTAL);

    return childLinearLayout;
}

您尝试将现有布局添加到滚动视图时出现错误,因此请尝试将滚动视图添加到您现有的布局

不知道它是否满足您的要求。

试试这个方法

public void drawer() {
        String[] word=s.split(" ");
        ScrollView  scrollView = new ScrollView(this);
        mainlinearLayout_ = (LinearLayout) findViewById(R.id.LinLay);
        mainlinearLayout_.removeAllViews();
        scrollView.removeAllViews();
        mainlinearLayout_.addView(scrollView);
        LinearLayout  verticalLinearLayout = new  LinearLayout(this);
        verticalLinearLayout.setOrientation(LinearLayout.VERTICAL);
        for (int j = 0; j < 3; j++) {
            LinearLayout horizontalChildLinearLayout = new LinearLayout(this);
            horizontalChildLinearLayout.setOrientation(LinearLayout.HORIZONTAL);

            for (int i = 1; i < word.length; i++) {
                final TextView textView = new TextView(this);
                textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                textView.setTextSize(17);
                textView.setPadding(5, 5, 5, 5);
                textView.setText(word[i - 1].toString());


                textView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(getApplicationContext(), textView.getText().toString(), Toast.LENGTH_LONG).show();
                    }
                });
                horizontalChildLinearLayout.addView(textView);
            }
            verticalLinearLayout.addView(horizontalChildLinearLayout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        }
        scrollView.addView(verticalLinearLayout);
    }

谢谢。