的setText(之差)和append() [英] Difference between setText() and append()

查看:598
本文介绍了的setText(之差)和append()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇的区别的setText()和append()创建。我正在写行号一个非常基本的编辑器。我有一个TextView到左侧,对保持数据的权限一个EditText配对持有行号。这里的XML:

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:重力=顶>
    <的TextView
        机器人:ID =@ + ID / line_numbers
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_marginRight =0dip
        机器人:重力=顶
        机器人:TEXTSIZE =14sp
        机器人:文字颜色=#000000
        机器人:字体=等宽
        机器人:paddingLeft =0dp/>
    <的EditText
        机器人:ID =@ + ID /编辑器
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_weight =1
        安卓的inputType =TEXT | textMultiLine | textNoSuggestions
        机器人:imeOptions =actionNone
        机器人:重力=顶
        机器人:TEXTSIZE =14sp
        机器人:文字颜色=#000000
        机器人:字体=等宽/>
< / LinearLayout中>

忽略一些其他的事情我做,我碰到的最奇怪的是额外的间距是出现了,当我用追加()(假设事情已经初始化,所有的)。

这下面,与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);


解决方案

我想改变BufferType通过append方法EDITABLE造成意想不到的填充。
如果你想使用append方法而不是setText方法和移除填充,

您可以尝试通过将其取出。

  textView.setincludeFontPadding(假)

或添加此行到您的TextView在XML文件

 的android:includeFontPadding =假

希望这有助于。

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>

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).

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");

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");

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");

解决方案

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,

you can try to remove it by using

textView.setincludeFontPadding(false)

or adding this line to your textview in your xml file

android:includeFontPadding="false"

Hope this helps.

这篇关于的setText(之差)和append()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆