滚动textview是不可能的?

时间:2017-11-20 23:44:33

标签: android

我为Android制作一款迷你冒险游戏,以学习Android编程。但是我的textview包含有关游戏事件的信息不会滚动。我已经以编程方式完成了它。它为什么不滚动?我尝试了一切,我想得出结论,Android可以做到这一点,但到目前为止它只是错误。

我的观点看起来像这样。

enter image description here

我的代码可从my repo.

获取
         date  value
0  2008-03-01   35.4
1  2008-06-01   37.5
2  2008-09-01   42.7
3  2008-12-01   47.6
4  2009-03-01   50.1
5  2009-06-01   54.3
6  2009-09-01   55.8
7  2009-12-01   60.5
8  2010-03-01   64.6
9  2010-06-01   66.7

更新

2个答案,几条评论,没有任何作品。我尝试了一切。什么都行不通。

2 个答案:

答案 0 :(得分:1)

这可能是因为你没有添加scrollbars属性,如下所示:

<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>

然后设置movementMethod

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

<强>更新

在匿名类和初始化程序块的帮助下,这有点丑陋,但适用于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);

代码由https://stackoverflow.com/a/19649335/4758255https://stackoverflow.com/a/28036532/4758255

生成

字符串资源如下:

<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>

您应该将上述代码修改为您的布局。我以LinearLayout为例。

答案 1 :(得分:0)

布局

<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>

爪哇

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);

}

它滚动!

enter image description here