如何在android中的页面中显示长字符串?

时间:2014-09-14 08:49:22

标签: java android string textview

我有一个来自数据库的大字符串。我在TextView中用滚动显示它。

这是一种显示大文字的完全无聊的方法;我想在页面中显示数据。

有许多与此类似的问题,但我无法从中找到任何解决方案;它们不完整且偏离轨道。

挑战

  1. 如何计算适合屏幕的行数?
  2. 如何在子部分中拆分字符串以使每个子字符串适合页面?

1 个答案:

答案 0 :(得分:0)

我认为这应该可以解决问题, 1.它将拆分大字符串 2.根据需要在视图上显示

所以如果你不想使用Scroll View那么我尝试了一些东西,我希望它有所帮助

我完成了 1.将所需视图扩展到父视图

2.将大文本设置为Textview

3.检查其他文本视图是否在(可见)屏幕内

4.适当更新

主要活动类

公共类MainActivity扩展了Activity {

private TextView text1, text2;
private Context context;
private LinearLayout layout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    context = getLayoutInflater().getContext();
    layout = (LinearLayout) findViewById(R.id.parentLayout);
    setTextViews();
    // checkViewAndUpdate();
    layout.getViewTreeObserver().addOnGlobalLayoutListener(
            new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    checkViewAndUpdate();
                }
            });

}

private void checkViewAndUpdate() {

    Rect rect = new Rect();
    layout.getHitRect(rect);

    if (text1.getLocalVisibleRect(rect)) {
        Toast.makeText(context, "visible", Toast.LENGTH_LONG).show();
    } else {
        // update if not visible decreasing the view text size
        Toast.makeText(context, "Not visible", Toast.LENGTH_LONG).show();
    }
    if (text2.getLocalVisibleRect(rect)) {
        Toast.makeText(context, "visible", Toast.LENGTH_LONG).show();
    } else {
        // update if not visible decreasing the view text size
        Toast.makeText(context, "Not visible", Toast.LENGTH_LONG).show();
    }

}

private void setTextViews() {

    text1 = new TextView(context);
    text2 = new TextView(context);

    text1.setText("your link seems to work. where/when do u trying to call getXXXVisibleRect()? if u do it at onCreate your link seems to work. where/when do u trying to call getXXXVisibleRect()? if u do it at onCreate your link seems to work. where/when do u trying to call getXXXVisibleRect()? if u do it at onCreateyour link seems to work. where/when do u trying to call getXXXVisibleRect()? if u do it at onCreate ");
    text2.setText("text2");

    text1.setTextSize(30);
    text2.setTextSize(30);

    // layout.removeAllViews();

    layout.addView(text1);
    layout.addView(text2);
}

}