无法在布局中添加多个布局

时间:2014-11-12 04:32:00

标签: android android-linearlayout android-gridview android-gridlayout

我在android中创建一个支持水平和垂直滚动的应用程序。我已将 ViewGroup作为基础 。我在视图组中 加载了一个Vertical Scroll Bar (从FrameLayout扩展)。 垂直滚动条包含一个水平滚动条 (派生自FrameLayout)作为其子项。我的想法是在Horizo​​ntalScrollbar中添加一个LinearLayout ,这样我就可以线性添加多个子节点。但是我无法在我在Horizo​​ntalScrollBar中添加的布局中添加多个LinearLayout  loading one LinearLayout within the Layout present inside the Horizontal scroll Bar.

Error when Loading more than one LinearLayout within the layout present inside the Horizontal Scroll Bar

我试过这种方式。这是我的代码:

     private VScrollViewer Vscroll;
    //VScrollViewer is a class extended from android.widget.ScrollView

    public AndroidGrid(Context paramContext)
    {
        super(paramContext);
        init();
    }

    public void init()
    {
        Vscroll = new VScrollViewer(getContext());
        LinearLayout lr=new LinearLayout(getContext());
        LinearLayout lr1=new LinearLayout(getContext());
        LinearLayout lr2=new LinearLayout(getContext());
        lr.setOrientation(LinearLayout.VERTICAL);
        lr1.setOrientation(LinearLayout.VERTICAL);
        lr2.setOrientation(LinearLayout.VERTICAL);
        lr.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        LinearLayout hlr= new LinearLayout(getContext());
        hlr.setOrientation(LinearLayout.HORIZONTAL);
        for(int i = 20; i < 550; i+=20){
            TextView myText = new TextView(getContext());
            myText.setY(i*3);
            myText.setText("HELLLLLOOOO");
            lr.addView(myText);
            lr1.addView(myText);
            lr2.addView(myText);
        }
        hlr.addView(lr);
        hlr.addView(lr1);
        hlr.addView(lr2);
        Vscroll.container.addView(hlr);
        addView(Vscroll);
    }

错误日志:

11-12 10:04:57.031: E/AndroidRuntime(18425): FATAL EXCEPTION: main
11-12 10:04:57.031: E/AndroidRuntime(18425): Process: com.example.baseexample, PID: 18425
11-12 10:04:57.031: E/AndroidRuntime(18425): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.baseexample/com.example.baseexample.MainActivity}: android.view.InflateException: Binary XML file line #12: Error inflating class com.example.baseexample.AndroidGrid
11-12 10:04:57.031: E/AndroidRuntime(18425):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
11-12 10:04:57.031: E/AndroidRuntime(18425):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
11-12 10:04:57.031: E/AndroidRuntime(18425):    at android.app.ActivityThread.access$800(ActivityThread.java:139)
11-12 10:04:57.031: E/AndroidRuntime(18425):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
11-12 10:04:57.031: E/AndroidRuntime(18425):    at android.os.Handler.dispatchMessage(Handler.java:102)
11-12 10:04:57.031: E/AndroidRuntime(18425):    at android.os.Looper.loop(Looper.java:136)
11-12 10:04:57.031: E/AndroidRuntime(18425):    at android.app.ActivityThread.main(ActivityThread.java:5086)
11-12 10:04:57.031: E/AndroidRuntime(18425):    at java.lang.reflect.Method.invokeNative(Native Method)
11-12 10:04:57.031: E/AndroidRuntime(18425):    at java.lang.reflect.Method.invoke(Method.java:515)
11-12 10:04:57.031: E/AndroidRuntime(18425):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
11-12 10:04:57.031: E/AndroidRuntime(18425):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
11-12 10:04:57.031: E/AndroidRuntime(18425):    at dalvik.system.NativeStart.main(Native Method)
11-12 10:04:57.031: E/AndroidRuntime(18425): Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class com.example.baseexample.AndroidGrid
11-12 10:04:57.031: E/AndroidRuntime(18425):    at android.view.LayoutInflater.createView(LayoutInflater.java:620)
11-12 10:04:57.031: E/AndroidRuntime(18425):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
11-12 10:04:57.031: E/AndroidRuntime(18425):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
11-12 10:04:57.031: E/AndroidRuntime(18425):    at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
11-12 10:04:57.031: E/AndroidRuntime(18425):    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
11-12 10:04:57.031: E/AndroidRuntime(18425):    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
11-12 10:04:57.031: E/AndroidRuntime(18425):    at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)
11-12 10:04:57.031: E/AndroidRuntime(18425):    at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
11-12 10:04:57.031: E/AndroidRuntime(18425):    at com.example.baseexample.MainActivity.onCreate(MainActivity.java:14)
11-12 10:04:57.031: E/AndroidRuntime(18425):    at android.app.Activity.performCreate(Activity.java:5248)
11-12 10:04:57.031: E/AndroidRuntime(18425):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
11-12 10:04:57.031: E/AndroidRuntime(18425):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
11-12 10:04:57.031: E/AndroidRuntime(18425):    ... 11 more
11-12 10:04:57.031: E/AndroidRuntime(18425): Caused by: java.lang.reflect.InvocationTargetException
11-12 10:04:57.031: E/AndroidRuntime(18425):    at java.lang.reflect.Constructor.constructNative(Native Method)
11-12 10:04:57.031: E/AndroidRuntime(18425):    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
11-12 10:04:57.031: E/AndroidRuntime(18425):    at android.view.LayoutInflater.createView(LayoutInflater.java:594)
11-12 10:04:57.031: E/AndroidRuntime(18425):    ... 22 more
11-12 10:04:57.031: E/AndroidRuntime(18425): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
11-12 10:04:57.031: E/AndroidRuntime(18425):    at android.view.ViewGroup.addViewInner(ViewGroup.java:3564)
11-12 10:04:57.031: E/AndroidRuntime(18425):    at android.view.ViewGroup.addView(ViewGroup.java:3417)
11-12 10:04:57.031: E/AndroidRuntime(18425):    at android.view.ViewGroup.addView(ViewGroup.java:3362)
11-12 10:04:57.031: E/AndroidRuntime(18425):    at android.view.ViewGroup.addView(ViewGroup.java:3338)
11-12 10:04:57.031: E/AndroidRuntime(18425):    at com.example.baseexample.AndroidGrid.init(AndroidGrid.java:55)
11-12 10:04:57.031: E/AndroidRuntime(18425):    at com.example.baseexample.AndroidGrid.<init>(AndroidGrid.java:28)
11-12 10:04:57.031: E/AndroidRuntime(18425):    ... 25 more

有人可以建议我在LinearLayout hlr中添加LinearLayout lr1和lr2吗?

2 个答案:

答案 0 :(得分:2)

Logcat说:

11-12 10:04:57.031: E/AndroidRuntime(18425): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

问题在于:

for(int i = 20; i < 550; i+=20){
    TextView myText = new TextView(getContext());
    myText.setY(i*3);
    myText.setText("HELLLLLOOOO");
    lr.addView(myText);
    lr1.addView(myText);
    lr2.addView(myText);
}

您正尝试将相同的TextView添加到三个布局。您应该为每个布局创建一个新的TextView,如下所示:

for(int i = 20; i < 550; i+=20){
    TextView myText1 = new TextView(getContext());
    TextView myText2 = new TextView(getContext());
    TextView myText3 = new TextView(getContext());

    myText1.setText("HELLLLLOOOO text 1");
    myText1.setText("HELLLLLOOOO text 2");
    myText1.setText("HELLLLLOOOO text 3");
    lr.addView(myText1);
    lr1.addView(myText2);
    lr2.addView(myText3);
}

希望这有帮助。

答案 1 :(得分:1)

问题是您无法在scrollview中拥有多个子项。如果你想做同样的事情,那么只需在View中创建两个ScrollViews或两个包含ScrollView的LinearLayout。

您还可以在Horizo​​ntalScrollView中使用Scrollview。你不需要那样做。如果您需要Horizo​​ntalScrollView然后删除子ScrollView,它将具有相同的效果。

希望这会有所帮助。快乐的编码:)

P.S。如果这个答案有帮助,请将其标记为正确答案。