我很好奇setText()和append()之间的区别。我正在编写一个非常基本的带有行号的编辑器。我有一个TextView在左侧用于保存行号,右侧是一个EditText用于保存数据。以下是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()时出现额外的间距(假设已经初始化了所有东西)。
以下内容与XML结合使用,在TextView和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 前面都会有右侧填充。
lineNumbers.append(String.valueOf(theLineCount)+"\n");
这并不是世界末日。但我很好奇是什么导致了这种行为。由于我对这门语言还很陌生,唯一能想到的可能就是在追加时,它将可编辑内容添加到其中并增加了填充。如果我能得到答案,我就可以用更简单的追加方法替换掉所有这些麻烦的代码:
lineNumbers.setText(lineNumbers.getText().toString()+String.valueOf(newLineCount)+"\n");