setText() 和 append() 的区别

setText() 和 append() 的区别

问题描述:

我对 setText() 和 append() 产生的差异很好奇.我正在编写一个带有行号的非常基本的编辑器.我有一个 TextView 在左边保存行号,在右边有一个 EditText 来保存数据.这是 XML:

I'm curious about the difference setText() and append() are creating. I'm writing a very basic editor with line numbers. I have a TextView to hold line numbers on the left, paired with an EditText on the right to hold the data. Here's the XML:

<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="match_parent" 
    android:gravity="top">
    <TextView
        android:id="@+id/line_numbers"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="0dip"
        android:gravity="top"
        android:textSize="14sp"
        android:textColor="#000000"
        android:typeface="monospace"
        android:paddingLeft="0dp"/>
    <EditText
        android:id="@+id/editor"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:inputType="text|textMultiLine|textNoSuggestions"
        android:imeOptions="actionNone"
        android:gravity="top"
        android:textSize="14sp"
        android:textColor="#000000"
        android:typeface="monospace"/>
</LinearLayout>

忽略我正在做的其他一些事情,我遇到的最奇怪的事情是当我使用 append() 时出现的额外间距(假设事情已经初始化等等).

Ignoring some of the other things I'm doing, the most curious thing I came across was the extra spacing that showed up when I used append() (assuming things have been initialized and all that).

下面结合 XML,在 TextView 和 EditText 之间设置了一个齐平的边框.

This below, in combination with the XML, sets a flush border between the TextView and EditText.

theEditor = (EditText) findViewById(R.id.editor);
lineNumbers = (TextView) findViewById(R.id.line_numbers);
theLineCount = theEditor.getLineCount();
lineNumbers.setText(String.valueOf(theLineCount)+"\n");

不过,将最后一行更改为此,TextView 中的每一行突然在 EditText 之前的右侧都有填充.

Change the last line to this, though, and suddenly each line in the TextView has padding on the right before the EditText.

lineNumbers.append(String.valueOf(theLineCount)+"\n");

这不是世界末日.但我很好奇是什么导致了这种行为.由于我是该语言的新手,我唯一能想到的可能是,当 append 在那里抛出 Editable 时,它​​会添加填充.如果我能得到答案,我就可以用更简单的附加内容替换所有这些讨厌的行:

It's not the end of the world. but I was curious what was causing this behavior. Since I'm new to the language, the only thing I could think of was maybe, when append throws the Editable on there, it adds the padding. If I can get an answer, I get to replace all of these nasty lines with simpler appends:

lineNumbers.setText(lineNumbers.getText().toString()+String.valueOf(newLineCount)+"\n");

我认为通过 append 方法将 BufferType 更改为 EDITABLE 会导致意外填充.如果您想使用 append 方法而不是 setText 方法并删除该填充,

I think changing BufferType to EDITABLE by append method caused the unexpected padding. If you want to use append method instead of setText method and remove that padding,

您可以尝试使用

textView.setincludeFontPadding(false)

或将此行添加到您的 xml 文件中的 textview

or adding this line to your textview in your xml file

android:includeFontPadding="false"

希望这会有所帮助.