简体   繁体   English

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

[英]How to show long Strings in pages in android?

I have a large String from the database. 我有一个来自数据库的大字符串。 I showed it in the TextView with scrolling. 我在滚动显示的TextView中显示了它。

This is a totally boring method to show large text though; 但是,这是一种显示大文本的完全无聊的方法。 I want to show data in pages. 我想在页面中显示数据。

There are multiple questions similar to this one, but I am not able to find any solutions from them; 有很多与此问题类似的问题,但是我无法从中找到任何解决方案。 they are incomplete and off track. 它们不完整且偏离轨道。

Challenges 挑战

  1. How to count number of lines that fit to screen? 如何计算适合屏幕的行数?
  2. How to split string in sub parts so that each substring fits to a page? 如何在子部分中分割字符串,以使每个子字符串适合页面?

I think this should solve the problem, 1. it will split the large string 2. display on view as you want 我认为这应该可以解决问题,1.它将拆分大字符串2.根据需要在视图上显示

So if u dont want to use Scroll View then well i tried something and i hope it helps 因此,如果您不想使用滚动视图,那么我尝试了一些事情,希望对您有所帮助

Following i done 1. inflate required views into parent view 完成之后1.将所需的视图充入父视图

2. set large text into Textview 2.将大文本设置为Textview

3. Check if other text views are within the (visible) screen or not 3.检查其他文本视图是否在(可见)屏幕内

4. update appropriately 4.适当更新

Main Activity class 主要活动课

public class MainActivity extends Activity { 公共类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); } 

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM