如何在android中滚动TextView

时间:2016-01-11 11:17:42

标签: android textview scrollview

我有以下代码块,我从stackoverflow本身参考了

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:isScrollContainer="true">
<!-- Include Action Bar -->
<include layout="@layout/actionbar_layout" />

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="900px"
    android:scrollbars="vertical"
    android:layout_x="0px"
    android:layout_y="25px"
    android:layout_marginTop="40dp">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:lines="90"
        android:layout_alignParentRight="true"
        android:scrollbars="vertical"
        android:layout_marginTop="22dp"
        android:text="Web Designing\n-Rs.30/-\n\nTechno Hunt\n-Rs.30/-\n\nDebugging\n-Rs.30/-\n\nGoogle Me\n-Rs.30/-\n\nCryptography\n-Rs.30/-\n\nQuery Master\n-Rs.30/-\n\nNeed For Speed: Most Wanted\n-Rs.50/-\n\nCounter Strike 1.6\n-Rs.150/-\n\nFifa-15\n-Rs.50/-\n\nBlind Typing\n-Rs.30/-\n\nAsphalt 8: Airborne\n-Rs.50/-\n\nLogo Designing\n-Rs.30/-\n\nPC Assembling\n-Rs.30/-\n\nVirtual Hunting\n-Rs.150/-\n\nRC-Maze\n-Rs.50/-\n\nFootball Skills\n-Rs.50/-\n\nBeg Borrow Steal\n-Rs.60/-\n\nTreasure Hunt\n-Rs.100/-\n\n60 Seconds To Fame\n-Rs.30/-\n\nPlay Fix Win\n-Rs.80/-\n\nPhotography\n-Rs.50/-\n\nChess\n-Rs.50/-\n\nCarrom Singles\n-Rs.30/-\n\nCarrom Doubles\n-Rs.60/-\n\nTable Tennis Singles\n-Rs.40/-\n\nTable Tennis Doubles\n-Rs.80/-\n\nCricket\n-Rs.700/-\n\nRink Football\n-Rs.200/-\n\nTug Of War\n-Rs.250/-\n\nBadminton Singles\n-Rs.50/-\n\nBadminton Doubles\n-Rs.100/-\n\nKabaddi\n-Rs.500/-"/>

</ScrollView>

活动看起来像这样

public class EntryFeeActivity  extends Activity {

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.entry_fee_layout);
    ScrollView scroller=new ScrollView(this);
    TextView tv=(TextView)findViewById(R.id.textView1);
    scroller.addView(tv);
    tv.setMovementMethod(new ScrollingMovementMethod());
   }
}

代码看起来很好......它还在预览窗格中提供了正确的预览 图片链接:http://imgur.com/t7NslEJ

但我仍然在运行应用时遇到此错误

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.manthan.bvimit.manthan16/com.manthan.bvimit.manthan16.EntryFeeActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2373)
                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2435)
                                                   at android.app.ActivityThread.access$900(ActivityThread.java:153)
                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1324)
                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                   at android.os.Looper.loop(Looper.java:135)
                                                   at android.app.ActivityThread.main(ActivityThread.java:5375)
                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                   at java.lang.reflect.Method.invoke(Method.java:372)
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:911)
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
                                                Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
                                                   at android.view.ViewGroup.addViewInner(ViewGroup.java:3937)
                                                   at android.view.ViewGroup.addView(ViewGroup.java:3787)
                                                   at android.widget.ScrollView.addView(ScrollView.java:278)
                                                   at android.view.ViewGroup.addView(ViewGroup.java:3728)
                                                   at android.widget.ScrollView.addView(ScrollView.java:260)
                                                   at android.view.ViewGroup.addView(ViewGroup.java:3701)
                                                   at android.widget.ScrollView.addView(ScrollView.java:251)
                                                   at com.manthan.bvimit.manthan16.EntryFeeActivity.onCreate(EntryFeeActivity.java:22)
                                                   at android.app.Activity.performCreate(Activity.java:6865)
                                                   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2326)
                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2435) 
                                                   at android.app.ActivityThread.access$900(ActivityThread.java:153) 
                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1324) 
                                                   at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                   at android.os.Looper.loop(Looper.java:135) 
                                                   at android.app.ActivityThread.main(ActivityThread.java:5375) 
                                                   at java.lang.reflect.Method.invoke(Native Method) 
                                                   at java.lang.reflect.Method.invoke(Method.java:372) 
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:911) 
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

当我点击活动图标

时,应用程序崩溃了

4 个答案:

答案 0 :(得分:0)

以这种方式做到:

你的xml应该是这样的

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

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />

    </LinearLayout>
</ScrollView>

 然后像这样处理它:

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.entry_fee_layout);
ScrollView scroller=(ScrollView )findViewById(R.id.scrollView1);
TextView tv=(TextView)findViewById(R.id.textView1);

tv.setMovementMethod(new ScrollingMovementMethod());
 }

答案 1 :(得分:0)

试试这个xml代码

使用android:layout_width =“fill_parent和android:layout_height =”fill_parent“ 而不是match_parent ..

不确定,但我希望它对你有用 试试吧

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<ScrollView
android:id="@+id/SCROLLER_ID"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:fillViewport="true">

    <TextView
    android:id="@+id/TEXT_STATUS_ID"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1.0"/>

</ScrollView>

答案 2 :(得分:0)

只需添加此ScrollView scroller=(ScrollView )findViewById(R.id.scrollView1); 代替ScrollView scroller=new ScrollView(this);并将{id}分配给ScrollView

答案 3 :(得分:0)

类别:

public class EntryFeeActivity extends Activity {

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.entry_fee_layout);
}

}

XML:

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

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="900px"
    android:layout_marginTop="40dp"
    android:scrollbars="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_marginTop="22dp"
            android:lines="90"
            android:scrollbars="vertical"
            android:text="Web Designing\n-Rs.30/-\n\nTechno Hunt\n-Rs.30/-\n\nDebugging\n-Rs.30/-\n\nGoogle Me\n-Rs.30/-\n\nCryptography\n-Rs.30/-\n\nQuery Master\n-Rs.30/-\n\nNeed For Speed: Most Wanted\n-Rs.50/-\n\nCounter Strike 1.6\n-Rs.150/-\n\nFifa-15\n-Rs.50/-\n\nBlind Typing\n-Rs.30/-\n\nAsphalt 8: Airborne\n-Rs.50/-\n\nLogo Designing\n-Rs.30/-\n\nPC Assembling\n-Rs.30/-\n\nVirtual Hunting\n-Rs.150/-\n\nRC-Maze\n-Rs.50/-\n\nFootball Skills\n-Rs.50/-\n\nBeg Borrow Steal\n-Rs.60/-\n\nTreasure Hunt\n-Rs.100/-\n\n60 Seconds To Fame\n-Rs.30/-\n\nPlay Fix Win\n-Rs.80/-\n\nPhotography\n-Rs.50/-\n\nChess\n-Rs.50/-\n\nCarrom Singles\n-Rs.30/-\n\nCarrom Doubles\n-Rs.60/-\n\nTable Tennis Singles\n-Rs.40/-\n\nTable Tennis Doubles\n-Rs.80/-\n\nCricket\n-Rs.700/-\n\nRink Football\n-Rs.200/-\n\nTug Of War\n-Rs.250/-\n\nBadminton Singles\n-Rs.50/-\n\nBadminton Doubles\n-Rs.100/-\n\nKabaddi\n-Rs.500/-" />
    </LinearLayout>
</ScrollView>