Android TextView中的文字单词之间有很大的间距。

huangapple go评论91阅读模式
英文:

Android text in textview have long distance between words

问题

我从API中获取到如下长描述文本:

{"status":true,"data":[{"id":674,"product_category_id":1,"child_category_id":1,"child_category_two_id":null,"child_category_three_id":null,"supplier_id":2,"supplier_name":"\u0627\u0644\u0645\u062f\u064a\u0631 \u0627\u0644\u0639\u0627\u0645","option_category_id":null,"tax_id":1,"barcode":0,"low_price":114,"image":"1586519373.jpeg","cost":115,"name_ar":"\u0633\u0644\u0629 \u0627\u0645\u0633\u0648 \u0627\u0644\u063a\u0630\u0627\u0626\u064a\u0629","name_en":"Amso food basket","details_ar":"...\",...,"details_en":"...\",...],"last_page":1,"current_page":1,"per_page":16}

但问题是,当我在TextView中显示时,出现了奇怪的结果:

Android TextView中的文字单词之间有很大的间距。
Android TextView中的文字单词之间有很大的间距。

从图中可以看出,在阿拉伯语中,几乎每个单词之间都有空格,但在英语中,出现在第1、2和3行之前的一些单词上。

以下是位于ConstraintLayout中的TextView的代码:

<TextView
    android:id="@+id/tv_activity_detail_description"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:paddingStart="8dp"
    android:paddingEnd="8dp"
    android:paddingBottom="4dp"
    android:paddingTop="4dp"
    android:textSize="12dp"
    android:textColor="@color/colorDarkGray"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/cardView" />
英文:

I'm getting long description text from the api like this

  {&quot;status&quot;:true,&quot;data&quot;:[{&quot;id&quot;:674,&quot;product_category_id&quot;:1,&quot;child_category_id&quot;:1,&quot;child_category_two_id&quot;:null,&quot;child_category_three_id&quot;:null,&quot;supplier_id&quot;:2,&quot;supplier_name&quot;:&quot;\u0627\u0644\u0645\u062f\u064a\u0631 \u0627\u0644\u0639\u0627\u0645&quot;,&quot;option_category_id&quot;:null,&quot;tax_id&quot;:1,&quot;barcode&quot;:0,&quot;low_price&quot;:114,&quot;image&quot;:&quot;1586519373.jpeg&quot;,&quot;cost&quot;:115,&quot;name_ar&quot;:&quot;\u0633\u0644\u0629 \u0627\u0645\u0633\u0648 \u0627\u0644\u063a\u0630\u0627\u0626\u064a\u0629&quot;,&quot;name_en&quot;:&quot;Amso food basket&quot;,&quot;details_ar&quot;:&quot;\u0648\u0627\u062d\u062f \u0643\u064a\u0633 \u0623\u0631\u0632 \u0623\u0628\u0648 \u0643\u0627\u0633  \u062e\u0645\u0633\u0629 \u0643\u064a\u0644\u0648\r\n\u0648\u0627\u062d\u062f \u062d\u0644\u064a\u0628 \u0628\u0648\u062f\u0631\u0629 \u0627\u0644\u0641 \u0648\u062b\u0645\u0627\u0646\u0645\u0627\u0626\u0629 \u062c\u0631\u0627\u0645\r\n\u0648\u0627\u062f \u0632\u064a\u062a \u0627\u0644\u0641 \u0648\u0633\u062a\u0645\u0627\u0626\u0629 \u062c\u0645\r\n\u0648\u0627\u062d\u062f \u0634\u0639\u064a\u0631\u064a\u0629 \u0627\u0631\u0628\u0639\u0645\u0627\u0626\u0629 \u0648\u062e\u0645\u0633\u0648\u0646 \u062c\u0645\r\n\u0648\u0627\u062d\u062f \u0645\u0639\u062c\u0648\u0646 \u0637\u0645\u0627\u0637\u0645 \u062b\u0645\u0627\u0646\u0645\u0626\u0629 \u0648\u0639\u0634\u0631 \u062c\u0645\r\n\u062b\u0644\u0627\u062b \u0639\u0644\u0628 \u0641\u0648\u0644 \u0645\u062f\u0645\u0633 \u0627\u0644\u0641 \u0648\u0645\u0627\u0626\u062a\u064a\u0646 \u062c\u0645\r\n\u062b\u0644\u0627\u062b \u0639\u0644\u0628 \u062a\u0648\u0646\u0629 \u062e\u0645\u0633\u0645\u0627\u0626\u0629 \u0648\u062e\u0645\u0633\u0648\u0646 \u062c\u0645\r\n\u0648\u0627\u062d\u062f \u0633\u0643\u0631 \u0627\u062b\u0646\u064a\u0646 \u0643\u064a\u0644\u0648\r\n\u0627\u062b\u0646\u064a\u0646 \u0645\u0639\u0643\u0631\u0648\u0646\u0629 \u0645\u062a\u0646\u0648\u0639\u0647 \u0627\u0644\u0641 \u062c\u0645&quot;,&quot;details_en&quot;:&quot;one Abu kas rice five kg   one powdered milk on thousand and eight hundred gm one vegetable oil on thousand and six hundred gm one fine noodles four hundred fifty gm  one tomato paste eight hundred and ten gm three beans cans   three tuna cans  one sugar two Kg two  mixed pasta one kg&quot;,&quot;sumInv&quot;:500,&quot;campaign&quot;:{&quot;id&quot;:null,&quot;product_id&quot;:null,&quot;price&quot;:null,&quot;purchasesLimits&quot;:null,&quot;stock&quot;:null},&quot;packages&quot;:[{&quot;package_id&quot;:2436,&quot;package_price&quot;:&quot;114.2900&quot;,&quot;unit_count&quot;:1,&quot;existing&quot;:0,&quot;virtual&quot;:500}]}],&quot;last_page&quot;:1,&quot;current_page&quot;:1,&quot;per_page&quot;:16}

, but the problen when I displayed in textview I got this weird result

Android TextView中的文字单词之间有很大的间距。
Android TextView中的文字单词之间有很大的间距。

As it appears it looks different in Arabic and English, in Arabic the spaces almost between every word but in english it happens in line 1,2 and 3 before some words.

here is the code on the textview which container is a constraintlayout

&lt;TextView
            android:id=&quot;@+id/tv_activity_detail_description&quot;
            android:layout_width=&quot;0dp&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:paddingStart=&quot;8dp&quot;
            android:paddingEnd=&quot;8dp&quot;
            android:paddingBottom=&quot;4dp&quot;
            android:paddingTop=&quot;4dp&quot;
            android:textSize=&quot;12dp&quot;
            android:textColor=&quot;@color/colorDarkGray&quot;
            app:layout_constraintEnd_toEndOf=&quot;parent&quot;
            app:layout_constraintHorizontal_bias=&quot;1.0&quot;
            app:layout_constraintStart_toStartOf=&quot;parent&quot;
            app:layout_constraintTop_toBottomOf=&quot;@+id/cardView&quot; /&gt;

答案1

得分: 1

我发现了Unicode字符的问题,所以当我尝试使用其Unicode字符来显示文本时,它会给我当前的屏幕,但我得到了这个Unicode的真实文本,并且它显示得很好,不管怎样,我通过在Java中将文本作为HTML来使用文本。我从这个答案中找到了解决方案:

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
    textView.setText(Html.fromHtml(user.getInput(), Html.FROM_HTML_MODE_LEGACY));
} else {
    textView.setText(Html.fromHtml(user.getInput()));
}
英文:

I found the problem with the unicode characters so when I tried to display the text using it's unicode characters it gives me the current screen but I get the real text of this Unicode and it displayed fine, anyway I find the solution by using the text as HTML in java:

if (android.os.Build.VERSION.SDK_INT &gt;= android.os.Build.VERSION_CODES.N) {
        textView.setText(Html.fromHtml(user.getInput(), Html.FROM_HTML_MODE_LEGACY));
    } else {
        textView.setText(Html.fromHtml(user.getInput()));
    }

I got the solution from this answer

huangapple
  • 本文由 发表于 2020年4月11日 06:23:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/61149541.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定