Creating LinearLayout Programmatically/Dynamically with Multiple Views

前端 未结 4 1214
太阳男子
太阳男子 2020-12-05 09:46

I have a hierarchy that is like this:

  • LinearLayout(horizontal)
    • ImageView
    • LinearLayout(vertical)
      • TextView
      • TextView
相关标签:
4条回答
  • 2020-12-05 10:13

    In the XML File LinearLayout already has child view. So there is not need to add them in code.

    0 讨论(0)
  • 2020-12-05 10:27

    The problem is because the views are already added to the layout in the XML file. Then you findViewById (find them) and try to add them to the layout again. That is why the app crashes complaining that, view already has a parent and you can't add it again.

    0 讨论(0)
  • 2020-12-05 10:30

    i suggest you to remove the xml file and just use full code on the java side. you can add the views programatically from the java side. this one from xtreemdeveloper but i change few line for the parent layout.

    // remove this params set it up below
    parent.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    
    // change the code above into 
    .LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
            addContentView(parent,layoutParams);
    // end of my change
    

    it will look like this in full code =

       LinearLayout parent = new LinearLayout(context);
    
        // remove this params set it up below
        parent.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    
        // change the code above into 
        .LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
                addContentView(parent,layoutParams);
        // end of my change
    
        parent.setOrientation(LinearLayout.HORIZONTAL);
    
        //children of parent linearlayout
    
        ImageView iv = new ImageView(context);
    
        LinearLayout layout2 = new LinearLayout(context);
    
        layout2.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
        layout2.setOrientation(LinearLayout.VERTICAL);
    
        parent.addView(iv);
        parent.addView(layout2);
    
        //children of layout2 LinearLayout
    
        TextView tv1 = new TextView(context);
        TextView tv2 = new TextView(context);
        TextView tv3 = new TextView(context);
        TextView tv4 = new TextView(context);
    
        layout2.addView(tv1);
        layout2.addView(tv2);
        layout2.addView(tv3);
        layout2.addView(tv4);
    
    0 讨论(0)
  • 2020-12-05 10:32

    You want that hierarchy programmatically.

    - LinearLayout(horizontal) - ImageView - LinearLayout(vertical) - TextView - TextView - TextView - TextView

    Ok lets start with Parent LinearLayout

    LinearLayout parent = new LinearLayout(context);
    
    parent.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    parent.setOrientation(LinearLayout.HORIZONTAL);
    
    //children of parent linearlayout
    
    ImageView iv = new ImageView(context);
    
    LinearLayout layout2 = new LinearLayout(context);
    
    layout2.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    layout2.setOrientation(LinearLayout.VERTICAL);
    
    parent.addView(iv);
    parent.addView(layout2);
    
    //children of layout2 LinearLayout
    
    TextView tv1 = new TextView(context);
    TextView tv2 = new TextView(context);
    TextView tv3 = new TextView(context);
    TextView tv4 = new TextView(context);
    
    layout2.addView(tv1);
    layout2.addView(tv2);
    layout2.addView(tv3);
    layout2.addView(tv4);
    

    And you are done :)

    0 讨论(0)
提交回复
热议问题