当文本在第二行换行时,TextView不换行

时间:2019-06-13 18:16:51

标签: android textview android-linearlayout android-textview-autosize

以下,当文本换行到第二行时,TextView为什么会有多余的空间(在下面的图像中用黄色框突出显示)?我该如何删除?

enter image description here

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="2"
        android:background="@color/green"
>

    <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:gravity="center"
            android:layout_weight="1"
        >

        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:padding="0dp"
                android:layout_gravity="center"
                android:text="THIS IS FOOOOOOOOOOOOOOO"
                android:background="@color/red"
        />
    </LinearLayout>

    <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:gravity="center"
            android:layout_weight="1"

    >

        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:padding="0dp"
                android:layout_gravity="center"
                android:text="THIS IS FOOOOOOOOOOOOOOO"
                android:background="@color/red"
        />
    </LinearLayout>

</LinearLayout>

2 个答案:

答案 0 :(得分:0)

可以尝试一些蛮力。首先将文本分成几行。循环播放它们,测量宽度,如果不超过textView的宽度,则追加到字符串生成器,如果大于textView的宽度,则逐字符测量,如果不超过,则追加到字符串生成器,否则在字符串生成器上追加“ \ n”。通过此构建器获取字符串并将其设置为textView。

答案 1 :(得分:0)

真正的原因是您为LinearLayout提供了 android:layout_weight 属性。根据发生的布局分离,移除android:layout_weight并进行如下检查

  <div class="contentImportat">
    <p class="description">Lorem ipsum dolor sit amet, consectetur adipiscing elit.
        Mauris eget accumsan ligula, id vehicula quam. Donec nec efficitur nisi.
        Morbi vel ultricies ligula.
    </p>
    <img class="znak" src="ogien.png">
</div>
<div class="contentCompany">
    <span>company: Lorem ipsum dolor</span>
    <span>street: Lorem ipsum dolor</span>
    <span>country: Lorem ipsum dolor</span>
    <span>phone: 921-123-131</span>
    <span>fax: 921-123-131</span>
</div>
<hr class="line">
<img class="img" src="ogien.png">

<?php
/**
 * @Route("/dom" , name="pdf_dom")
 */
public function domAction()
{
    $pdfOptions = new Options();
    $pdfOptions->set('defaultFont', 'Arial');

    // Instantiate Dompdf with our options
    $dompdf = new Dompdf($pdfOptions);

    $dompdf = new Dompdf(array('enable_remote' => true));

    // Retrieve the HTML generated in our twig file
    $html = $this->renderView('pdf/horismall.html.twig');

    // Load HTML to Dompdf
    $dompdf->loadHtml($html);

    // (Optional) Setup the paper size and orientation 'portrait' or 'portrait'
    $dompdf->setPaper('A4', 'portrait');

    // Render the HTML as PDF
    $dompdf->render();

    // Output the generated PDF to Browser (force download)
    $dompdf->stream("mypdf.pdf", [
        "Attachment" => false
    ]);
}