TextView перемещается на одну строку вниз, когда текст изменяется программно

Это простое приложение-оболочка, которое будет отображать количество файлов в папке 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);
    }
    }

Скриншоты:

https://stackru.com/images/6d647b2857e73f2e7582def4ff7a1f7c4df94b4f.png

https://stackru.com/images/24246b8d6c4d2bc297d8b7242f54bbd74fe7efad.png

2 ответа

Установите текст в textview после выполненной операции обрезки, возможно, это сработает, попробуйте

tv.setText(text.trim());

При просмотре очень быстро кажется, что возвращение команды идет с текстом.. Попробуйте заменить новую строку в text перед установкой текста в TextView.

Другие вопросы по тегам