android - 当文本以编程方式更改时,TextView 向下移动一行

标签 android textview

这是一个简单的 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);
    }
    }

截图:

http://i.stack.imgur.com/QgYoF.png

http://i.stack.imgur.com/8OjB6.png

最佳答案

裁剪后在textview中设置文本,也许这样可以,试一试

tv.setText(text.trim());

关于android - 当文本以编程方式更改时,TextView 向下移动一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10665001/

相关文章:

android - MonoTouch/MonoDroid 服务层不兼容?

php - 使用不同的 php 脚本获取 wp_loggedin 值,然后在 Android 应用程序中返回值

android - 带有嵌入式浏览器的 Google OAuth 2.0 和强制门户

android - 在 inflated layout 中使用 setText() 到 TextView() 后不显示文本

从右到左使用 TextView 的 Android 设置

java - Android:TextView 不工作

android - 主题继承在 Android 中究竟是如何工作的?

android - 在类似于 Facebook 时间线的回收站 View 中显示图像

android - 如何在 TextView 中的特定行获取文本

Android - 更改操作栏标题文本颜色