Is scrolling a textview impossible?

前端 未结 2 1836
一向
一向 2021-01-29 05:09

I make a mini adventure game for Android to learn Android programming. But my textview which contains information about the game events does not scroll. I have done it programma

相关标签:
2条回答
  • 2021-01-29 05:50

    That's probably because you didn't add scrollbars attributes with something like this:

    <TextView
        android:id="@+id/your_tv"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:scrollbars = "vertical"
        android:text="@string/your_really_long_string" >
    </TextView>
    

    Then set the movementMethod:

    TextView tv = (TextView) findViewById(R.id.your_tv);
    tv.setMovementMethod(new ScrollingMovementMethod());
    

    UPDATE

    This is a bit ugly hacks, with the help of an anonymous class and an initializer block, but works on Android 6.0:

    LinearLayout lly = (LinearLayout) findViewById(R.id.container);
    
    TextView textView = new TextView(this) {
      {
        setVerticalScrollBarEnabled(true);
        setMovementMethod(ScrollingMovementMethod.getInstance());
        setScrollBarStyle(View.SCROLLBARS_INSIDE_INSET);
    
        // Force scrollbars to be displayed.
        TypedArray a = this.getContext().getTheme().obtainStyledAttributes(new int[0]);
          // the initializeScrollbars is marked with @removed on API 21 but it still there.
          // call the initializeScrollbars with reflection.
          try {
            // initializeScrollbars(TypedArray)
            Method initializeScrollbars =
                android.view.View.class.getDeclaredMethod("initializeScrollbars", TypedArray.class);
            initializeScrollbars.invoke(this, a);
          } catch (NoSuchMethodException e) {
            e.printStackTrace();
          } catch (InvocationTargetException e) {
            e.printStackTrace();
          } catch (IllegalAccessException e) {
            e.printStackTrace();
          }
        a.recycle();
      }
    };
    
    lly.addView(textView);
    textView.setText(R.string.long_string);
    

    Code is generated from https://stackoverflow.com/a/19649335/4758255 and https://stackoverflow.com/a/28036532/4758255

    The string resource is the following:

    <resources>
      <string name="long_string">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum
      Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?
    
      At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet ut et voluptates repudiandae sint et molestiae non recusandae. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
    
      </string>
    </resources>
    

    You should modify the above code to your layout. I'm using a LinearLayout as an example.

    0 讨论(0)
  • 2021-01-29 06:00

    Layout

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    
        <!-- Some stuff goes here -->
    
        />
    
        <Button
            android:id="@+id/close"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_column="0"
            android:text="@string/dummy_content" />
    
        <Button
            android:id="@+id/close2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_column="1"
            android:text="@string/dummy_content" />
    
        <Button
            android:id="@+id/close3"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_column="2"
            android:text="@string/dummy_content" />
    
        <Button
            android:id="@+id/close4"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_column="3"
            android:text="@string/dummy_content" />
    
        <Button
            android:id="@+id/close5"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_column="4"
            android:text="@string/dummy_content" />
    
        <Button
            android:id="@+id/close6"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_column="5"
            android:text="@string/dummy_content" />
    
    </TableRow>
    
    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    
        <ScrollView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_span="6"
            android:layout_weight="1"
            android:scrollbars="none">
    
            <!-- Some stuff goes here -->
    
            />
    
            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:layout_span="6"
                android:background="#FFFFFF"
                android:gravity="bottom"
                android:maxLines="4"
                android:scrollbars="vertical"
                android:text="This is sample TextView Text with scrolling feature.This is sample TextView Text with scrolling feature.This is sample TextView Text with scrolling feature.This is sample TextView Text with scrolling feature.This is sample TextView Text with scrolling feature.This is sample TextView Text with scrolling feature.This is sample TextView Text with scrolling feature.This is sample TextView Text with scrolling feature.This is sample TextView Text with scrolling feature.This is sample TextView Text with scrolling feature.This is sample TextView Text with scrolling feature."
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textColor="#000000" />
    
        </ScrollView>
    
    </TableRow>
    
    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    
        <!-- Some stuff goes here -->
    
        />
    
        <dev.game.adventure.AdventureGame
            android:id="@+id/view"
            android:layout_span="6"
            android:background="#FF0000" />
    </TableRow>
    

    Java

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fullscreen);
       // vScroll = (ScrollView) findViewById(R.id.vScroll);
    
        scrollable = (TextView)findViewById(R.id.textView1);
    
        //Enabling scrolling on TextView.
        scrollable.setMovementMethod(new ScrollingMovementMethod());
    for(int i=0;i<100;i++)
        scrollable.append("q");
    
       // TableRow tr3=(TableRow)findViewById(R.id.tableRow3);
      //View v = findViewById(R.id.view);
        //ag = new AdventureGame((TableLayout)findViewById(R.id.tableLayout1), scrollable, this, 5, "Jamie", onScoreListener);
      // ag.setBackground(getResources().getDrawable(R.mipmap.dungeon));
      //  tr3.addView(ag);
    
       // new Adventure(scrollable, this, ag);
       // v = ag;
    
        AdventureGame ag = findViewById(R.id.view);
        ag.text = findViewById(R.id.textView1);
        ag = findViewById(R.id.view);
    
        new Adventure(scrollable, this, ag);
    
    }
    

    And it scrolls!

    0 讨论(0)
提交回复
热议问题