简体   繁体   中英

Combining views programmatically - Android

I've got a list of layouts that I need displayed on a PDF. However I'd like to find a way where I can combine these views into one view.

for(LinearLayout cardView : selectedCardIDList){
            pageNo++;
            cardView.measure(measuredWidth, 0);
            cardView.layout(0, 0, pageWidth, cardView.getHeight());
            cardView.draw(canvas);
        }

This is the method I use to draw each view. I'd like to combine the views and then draw the new view.

查看范例

Here's a quick example i drew up with 'V' being View.

On the left I have View 1 and View 2, I'd to try and stack them in a brand new view like View 3. Hope this helps

Here is an example of my problem. 这是我的问题的一个例子。

private void generatePDF(){
            PrintAttributes printAttributes = new PrintAttributes.Builder()
                    .setColorMode(PrintAttributes.COLOR_MODE_COLOR)
                    .setMediaSize(PrintAttributes.MediaSize.ISO_A4)
                    .setMinMargins(PrintAttributes.Margins.NO_MARGINS)
                    .setResolution(new PrintAttributes.Resolution("Res_Test", PRINT_SERVICE, 450, 700))
                    .build();

            PdfDocument document = new PrintedPdfDocument(getActivity(), printAttributes);

            PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(595,842,1).create();

            PdfDocument.Page page = document.startPage(pageInfo);

            for(LinearLayout cardView : selectedCardIDList){
        ((ViewGroup)cardView.getParent()).removeView(cardView);
        combiPDfView.addView(cardView);
    }
    combiPDfView.draw(canvas);

            document.finishPage(page);
    }

This is the method I'm using to generate the PDF.

<LinearLayout
            android:id="@+id/pdfView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:visibility="invisible"/>

This is the layout that I've made to add views to and display on the PDF. combiPDfView is the name of the LinearLayout.

You can implement this by using this:

Take a parent Linearlayout with the orientation vertical

LinearLayout parentView= findViewById(R.id.parentView); 

code to add the view

for(LinearLayout cardView : selectedCardIDList){
            // View to be added
            pageNo++;
            cardView.measure(measuredWidth, 0);
            cardView.layout(0, 0, pageWidth, cardView.getHeight());
            cardView.draw(canvas);
            //add view to parent view
            parentView.addView(cardView);
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM