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