英文:
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中显示时,出现了奇怪的结果:
从图中可以看出,在阿拉伯语中,几乎每个单词之间都有空格,但在英语中,出现在第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
{"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":"\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","details_en":"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","sumInv":500,"campaign":{"id":null,"product_id":null,"price":null,"purchasesLimits":null,"stock":null},"packages":[{"package_id":2436,"package_price":"114.2900","unit_count":1,"existing":0,"virtual":500}]}],"last_page":1,"current_page":1,"per_page":16}
, but the problen when I displayed in textview I got this weird result
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
<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" />
答案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 >= 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论