【问题标题】:TextView moves one line down when text changes programmatically当文本以编程方式更改时,TextView 向下移动一行
【发布时间】:2012-05-19 12:02:36
【问题描述】:

这是一个简单的 shell 应用程序,当单击按钮时将显示 data/app 文件夹中的文件数一条线。为什么?

这里是代码和main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_marginBottom="32dp"
    android:text="Button" />



<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textView2"
    android:text="@string/user_apps" />



<TextView
    android:id="@+id/tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/textView2"
    android:text="0" />

</RelativeLayout>

代码:

 package rs.test.rootapp;

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;

    public class RootAppActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final Button button=(Button) findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                String command[]={"su", "-c","ls /data/app | wc -l"};
                Shell shell = new Shell();
                String text = shell.sendShellCommand(command);
                setNewTextInTextView(text);
            }
        });

    }

    public void setNewTextInTextView(String text){
        //TextView tv= new TextView (this);
        //tv.setText(text);
        //setContentView(tv);
        TextView tv =(TextView) findViewById(R.id.tv);
        tv.setText(text);
    }
    }

截图:

【问题讨论】:

  • 更改文本视图的宽度

标签: android textview


【解决方案1】:

在文本视图中设置文本,完成修剪操作后,可能会这样,试试吧

tv.setText(text.trim());

【讨论】:

  • 我会试试这个,看起来比替换简单
  • 我也想投票,因为我帮忙了,我投票给你的答案;P
  • 答案没有给出原因,我的答案给出了原因,而您只是展示了另一种方式...此外,您以可能不需要的方式对输出进行修剪...他只想要新行关闭,仅在开始!
【解决方案2】:

看起来很快,命令的返回似乎与文本一起出现。在 TextView 中设置文本之前,尝试替换 text 中的新行。

【讨论】:

  • shell.sendShellCommand(command); 的返回是添加一个新行,否则我猜……
  • 我怎样才能删除新行?
  • 使用替换或子字符串....用你的头脑。另见stackoverflow.com/questions/6158123/…
  • 我终于明白了。感谢帮助。这就是我所做的。 String text2 = text.replace("\n" , "");
猜你喜欢
  • 2015-01-14
  • 1970-01-01
  • 1970-01-01
  • 2016-04-24
  • 2017-12-02
  • 1970-01-01
  • 2016-07-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多