Include same layout multiple times

前端 未结 4 1138
抹茶落季
抹茶落季 2021-02-13 21:56

I have a common layout (common.xml) which I want to include many times in another layout (layout_a.xml). But it only shows me just one time. Why?

common.xml

相关标签:
4条回答
  • 2021-02-13 22:17

    As btse said, the ids in the XML must be unique. It can be achieved in this way:

    <include android:id="@+id/common1"
        layout="@layout/common" />
    
    <include android:id="@+id/common2"
        layout="@layout/common" />
    

    For information about how to access the elements inside those two included views, you can check out this blog post.

    0 讨论(0)
  • 2021-02-13 22:23

    The ids when defined in XML must be unique. You are including two layouts that have views that contain the same id.

    Here is how you would go about fixing it.

    p.s. Unless there is more code that you are not including in your first layout file, that merge tag is useless.

    0 讨论(0)
  • 2021-02-13 22:34

    I fixed by setting the layout_height of the RelativeLayout to 250dp since they are overlapped.

    0 讨论(0)
  • 2021-02-13 22:36

    That's what I had done in my Project with Inflater. test1is just a Layout made with a LinearLayout(Vertical) with text and a button and mainofferslayout in that case, the main layout with an image.

    // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_offers_display, container, false);
    
        View inflatedLayout = inflater.inflate(R.layout.test1, (ViewGroup) view, false);
        LinearLayout ll = (LinearLayout) view.findViewById(R.id.mainofferslayout);
    
        ll.addView(inflater.inflate(R.layout.test1, (ViewGroup) view, false));
        ll.addView(inflater.inflate(R.layout.test1, (ViewGroup) view, false));
        ll.addView(inflater.inflate(R.layout.test1, (ViewGroup) view, false));
    
    0 讨论(0)
提交回复
热议问题