【问题标题】:Android TextView breaking lines incorrectlyAndroid TextView 断行不正确
【发布时间】:2016-09-06 04:53:09
【问题描述】:

我在 ListView 中使用 TextView 时遇到问题,其中 TextView 错误地换行。

为了说明问题,我要显示以下文本:

"12345678901234567 ="
"12345678901 ="
"12345678901234567 *"

我使用的xml(TextView)如下:-

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="end"
    android:textAlignment="gravity"
    android:layout_gravity="end"
    android:textAppearance="?android:attr/textAppearanceLarge"
/>

结果显示如下:

   123456789
    01234567
           =
12345678901=
   123456789
    01234567
           *

显然我不希望“=”和“*”位于单独的行上。我有 尝试了几十种不同的 xml 变体,但没有达到我想要的效果。

测试的模拟器是:

Nexus 6 API 23:5556

Nexus6p API 23:5556

MotoG3 Android 6

使用 23.0.3 为 Android 6 编译

使用以下 xml (EditText),我确实得到了想要的结果:

<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="end"
    android:textAlignment="gravity"
    android:layout_gravity="end"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:editable="false">
</EditText>

正确结果如下:

12345678901234
         567 =
 12345678901 =
12345678901234
         567 *

虽然我可以使用 EditText,但显示不应该是可编辑的 而且我相信 TextView 应该可以工作。此外,“机器人:可编辑” 已弃用。

有人可以告诉我如何获取 TextView 根据需要工作,或者如何使用 EditText 一个未弃用的功能并达到预期的结果?

.........添加到问题---------

在test prog中用于创建ListView的代码如下:-

private void createLayout() {

    RelativeLayout wRelLayout = new RelativeLayout(this);

    wRelLayout.setBackgroundColor(Color.DKGRAY);

    RelativeLayout.LayoutParams jLayParams = new RelativeLayout.LayoutParams(500, 700);

    setContentView(wRelLayout, jLayParams);

    ArrayList<String> alCalcLines = new ArrayList();
    ListView lvCalcLines = new ListView(this);
    lvCalcLines.setX(10);
    lvCalcLines.setY(10);
    lvCalcLines.setBackgroundColor(Color.WHITE);
    lvCalcLines.setPadding(10, 10, 10, 10);
    lvCalcLines.setAdapter(new ArrayAdapter<>(
            this, R.layout.text_view_1, alCalcLines));  // xml

    jLayParams = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.MATCH_PARENT);

    wRelLayout.addView(lvCalcLines, jLayParams);

    alCalcLines.add("12345678901234567 =");
    alCalcLines.add("12345678901 =");
    alCalcLines.add("12345678901234567 *");
}

【问题讨论】:

    标签: android xml android-layout


    【解决方案1】:

    将此添加到您的编辑文本 XML 中,对我有用。

    <EditText
        android:clickable="false" 
        android:cursorVisible="false" 
        android:focusable="false" 
        android:focusableInTouchMode="false"/>
    

    【讨论】:

    • 我可能会看,但是它是一个 TextView,TextView 应该正确处理它。
    【解决方案2】:

    在 Textview 或 edittext 中试试这些

     txt_view.setTextDirection(View.TEXT_DIRECTION_RTL);
    

     txt_view.setTextDirection(View.TEXT_DIRECTION_ANY_RTL);
    

    同样在 android:supportsRtl="true" 到应用程序

    在xml中

     android:textDirection="rtl"
    

    将重力设置为向右

    【讨论】:

      【解决方案3】:

      如果您不希望某个单词(在您的情况下“=”是单词,因为它用空格分隔)不转移到下一行,您可以使用 不间断空格.
      在 Android 中是 \u00A0。所以你的字符串看起来像这样:

      "12345678901234567\u00A0="
      "12345678901\u00A0="
      "12345678901234567\u00A0*"
      

      【讨论】:

      • 我把 "\u00A0" 放在 TextView 中,它似乎工作正常。默认总是在空格上中断吗?如果很快没有更好的答案,我会打勾。在我看来,如果需要休息,换行会更容易,但也许我错过了一些东西。
      • @briano,仅当第二个单词太长而无法容纳 TextView 时。如果回答正确,请采纳
      • 这里不是这样。它应该适合两行。
      • @briano,如果您显示放置 ListView 的 xml,我可以尝试找出它为什么会这样工作
      • 好的,谢谢,现在该走了。我稍后再做。它在代码中。
      【解决方案4】:

      在文本视图行中添加 [android:SingleLine="true"]

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-10-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-01
        • 1970-01-01
        • 2015-09-07
        相关资源
        最近更新 更多