以编程方式将视图添加到LinearLayout,但是它们不会出现 [英] Adding views programmatically to LinearLayout but they don't appear

查看:51
本文介绍了以编程方式将视图添加到LinearLayout,但是它们不会出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在simple_pdf_example.xml内的LinearLayout中填充10个printed_order_element2.xml,只是为了生成具有ListView(实际上是LinearLayout)的PDF.

I'm trying to populate a LinearLayout inside simple_pdf_example.xml with 10 printed_order_element2.xml just so I can generate a PDF with a ListView (which is actually a LinearLayout).

问题是当我执行linearLayoutView.addView(v) 10次时,在LinearLayout中看不到v.我只是看到我添加到xml中的原始项目,只是为了查看LinearLayout是否正在渲染.

The problem is that when I do linearLayoutView.addView(v) 10 times, I don't see v inside the LinearLayout. I just see the original item I added in the xml just to see if the LinearLayout was rendering.

SimplePDFSaver.java:

package com.mypackage.example;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.FileProvider;

import android.content.Intent;
import android.graphics.pdf.PdfDocument;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import java.io.File;
import java.io.FileOutputStream;


public class SimplePDFSaver extends AppCompatActivity {
    private static final String TAG = "SimplePDFSaver";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simple_pdfsaver);
        generatePDF();
    }

    public void generatePDF() {
        LayoutInflater inflater = getLayoutInflater();
        View pdfLayout = inflater.inflate(R.layout.simple_pdf_example,
            findViewById(android.R.id.content),
            false);


        int sizeSpec = View.MeasureSpec.makeMeasureSpec(2480, View.MeasureSpec.EXACTLY);
        int sizeSpec2 = View.MeasureSpec.makeMeasureSpec(3508, View.MeasureSpec.EXACTLY);
        pdfLayout.measure(sizeSpec, sizeSpec2);

        int width = pdfLayout.getMeasuredWidth();
        int height = pdfLayout.getMeasuredHeight();
        pdfLayout.layout(0, 0, width, height);

        LinearLayout linearLayoutView = pdfLayout.findViewById(R.id.linearLayoutView);

        for (int i=0; i<10; i++) {
            View v = getLayoutInflater().inflate(R.layout.printed_order_element2, null);
            linearLayoutView.addView(v);
        }

        Runnable r = new Runnable() {
            @Override
            public void run() {

                PdfDocument document = new PdfDocument();
                PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(2480, 3508, 0).create();
                PdfDocument.Page page = document.startPage(pageInfo);

                pdfLayout.draw(page.getCanvas());
                document.finishPage(page);

                FileOutputStream outStream;
                File file = new File(getExternalFilesDir(null), "file.pdf");
                try {
                    outStream = new FileOutputStream(file);
                    document.writeTo(outStream);
                    document.close();
                    outStream.flush();
                    outStream.getFD().sync();
                    outStream.close();

                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    Uri uri = FileProvider.getUriForFile(SimplePDFSaver.this, BuildConfig.APPLICATION_ID + ".provider", file);
                    intent.setDataAndType(uri, "application/pdf");
                    intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    startActivity(intent);
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.d(TAG, e.toString());
                }
            }
        };
        new Thread(r).start();
    }
}

simple_pdf_example.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/linearLayoutView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:orientation="vertical"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView10">


        <TextView
            android:id="@+id/textView13"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Just to see if linearLayoutView rendered" />
    </LinearLayout>

    <TextView
        android:id="@+id/textView10"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="There should be a list of items below:"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>

printed_order_element2.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView6"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView" />
</LinearLayout>

这就是我得到的:

推荐答案

我相信问题出在这部分代码中:

I believe the problem lies in this section of code:

public void generatePDF() {
    LayoutInflater inflater = getLayoutInflater();
    View pdfLayout = inflater.inflate(R.layout.simple_pdf_example,
            findViewById(android.R.id.content),
            false);

其余所有代码都使用这种夸大的pdfLayout视图:

All of the rest of your code uses this inflated pdfLayout view:

LinearLayout linearLayoutView = pdfLayout.findViewById(R.id.linearLayoutView);

for (int i=0; i<10; i++) {
    View v = getLayoutInflater().inflate(R.layout.printed_order_element2, null);
    linearLayoutView.addView(v);
}

pdfLayout.draw(page.getCanvas());

问题是您的pdfLayout视图从不显示在屏幕上.您在onCreate()中的setContentView()调用会展开不同的布局,并且展开后的pdfLayout不会附加到任何视图!

The problem is that your pdfLayout view is never put on the screen. Your setContentView() call in onCreate() inflates a different layout, and the inflated pdfLayout is never attached to any view!

当您调用inflate(int, View, boolean)并将false作为第三个参数传递时,您正在告诉系统将第二个参数(View)视为仅父级 以便进行解析LayoutParams在展开的布局的根目录上.夸大的布局不会添加到父项中!您必须使用展开视图手动调用parent.addView().

When you call inflate(int, View, boolean) and pass false as the third argument, you are telling the system to treat the second argument (the View) as the parent only in order to parse LayoutParams on the root of the inflated layout. The inflated layout is not added to the parent! You have to manually call parent.addView() with the inflated view.

如何解决?确切地说,很难说,因为我有几个问题.但是也许只是问问题就会揭示答案.

How to fix it? It's hard to say, exactly, since I've got a few questions. But maybe just asking the questions will reveal the answer.

看到两者都对setContentView()的调用和对inflate()的调用以android.R.id.content作为父级传递的消息很奇怪.您能只在onCreate()中调用setContentView(R.layout.simple_pdf_example),而完全避免第二个inflate()调用吗?

It's strange to see both a call to setContentView() and a call to inflate() that passes in android.R.id.content as the parent. Could you just call setContentView(R.layout.simple_pdf_example) in your onCreate(), and avoid the second inflate() call altogether?

如果是这样,那么您将所有对pdfLayout.findViewById()的调用替换为直接的findViewById()调用(因为以前的pdfLayout现在只是您活动的主要内容).

If so, then you'd replace all calls to pdfLayout.findViewById() with direct findViewById() calls (since what was previously pdfLayout is now just your Activity's main content).

这篇关于以编程方式将视图添加到LinearLayout,但是它们不会出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆