简体   繁体   English

以编程方式组合视图-Android

[英]Combining views programmatically - Android

I've got a list of layouts that I need displayed on a PDF. 我有一个需要在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. 这是我用“ V”作为视图绘制的一个简单示例。

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 左侧有View 1和View 2,我想尝试将它们堆叠在一个全新的视图(如View 3)中。希望这对您有所帮助

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. 这是我用来生成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. 这是我添加视图并显示在PDF上的布局。 combiPDfView is the name of the LinearLayout. combiPDfView是LinearLayout的名称。

You can implement this by using this: 您可以使用以下方法实现此目的:

Take a parent Linearlayout with the orientation vertical 以垂直方向获取父级Linearlayout

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);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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