android - TextView 滚动问题

标签 android scroll textview

你好

我正在编写一个显示正在运行的进程列表的应用程序,我正在使用 TextView 在屏幕上显示正在运行的进程。 我正在尝试 2 种不同的方法来显示 TextView。 在一种方法中,我在代码本身中清除了 TextView

TextView tv = new TextView(this);
this.setContentView(tv);

在第二种方法中,我清除了 main.xml 文件中的布局

setContentView(R.layout.main);
TextView tv=(TextView) findViewById(R.id.RunApp);

对于这两种方法,我都使用 setMovementMethod 为我的 TextView 进行滚动移动

tv.setMovementMethod(new ScrollingMovementMethod()); 

该程序在模拟器中运行良好。但如果我使用第二种方法,那么如果我在我的手机中运行该应用程序,则不会发生滚动。在模拟器中它工作正常。为什么它在我的 Android 2.1 Samsung Galaxy S 中表现不同? 为什么在第二种方法中没有发生滚动??。我的手机在第一种方法中发生滚动,没有任何问题。

这是我的代码..

public class listRunningApps extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.main);
    //TextView tv=(TextView) findViewById(R.id.RunApp);

    TextView tv = new TextView(this);
    this.setContentView(tv);

    tv.setMovementMethod(new ScrollingMovementMethod()); 

    ActivityManager actvityManager = (ActivityManager)
               this.getSystemService( ACTIVITY_SERVICE );
    List<RunningAppProcessInfo> procInfos = actvityManager.getRunningAppProcesses();

    for(int j=0; j < 2; j++) 
/*I have done iteration 2 times so that we have a long process list 
    sufficeint enough for scrolling*/
        {
        for(int i = 0; i < procInfos.size(); i++)
            {
                tv.setText(tv.getText().toString()+procInfos.get(i).processName+
                    " " + String.valueOf(procInfos.get(i).processName.length())+"\n");
            }

    tv.setText(tv.getText().toString()+"----------------------------"+"\n");
        }
     }
}

这是我的 main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TextView
        android:id="@+id/jsttxt"  
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Testing TextView Scrolling"/>

<TextView
    android:id="@+id/RunApp"
    android:layout_marginLeft="30px"
    android:layout_marginRight="30px"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/jsttxt"
    />

</RelativeLayout>

在第一种方法中,滚动在我的手机中没有任何问题。你们能帮我解释一下为什么在第二种方法中我的手机没有滚动吗,因为它在我的模拟器中工作正常?

最佳答案

为什么不将 TextView 放在 xml 布局的 ScrollView 中。尝试一下,我认为它有效

关于android - TextView 滚动问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3869414/

相关文章:

jquery - 视差滚动,每个类的循环脚本

滚动已经开始后,Javascript在touchmove上停止滚动

android - Android中如何设计段落样式的标题?

Android自定义图库 View ,设置自己的边框

java - 类型转换为 Dalvik 格式失败 : Unable to execute dex: java. nio.BufferOverflowException

android - 如何在Flutter中设置TextSpan的圆角

java - MySQL ResultSet 可滚动/可更新未按预期工作

android - 在android中显示分数

java - 如何自定义TextView中的链接?

android - Android 上的 Cordova 应用程序使用 SSL 发送多部分/表单数据导致机架应用程序错误 : #<EOFError: bad content body>