2014-01-08 70 views
0

我有一个包含32行文本的TextView。在旋转到横向时,TextView对于屏幕来说变得太大,因此我希望它分成2,16行TextViews,但不知道这是否可行。这是我迄今为止所拥有的。TextView对于屏幕太大

我知道我可以做一个测试,看看是否getHeight()>屏幕高度,但即使它是,我不知道该怎么办。

 TextView displayMethod = new TextView(getActivity()); 
     displayMethod.setTextColor(Color.BLACK); 
     displayMethod.setClickable(false); 
     displayMethod.setBackgroundColor(Color.WHITE); 
     displayMethod.setTextSize(14); 
     displayMethod.setTypeface(Typeface.MONOSPACE); 

     LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
     params.setMargins(10,20,10, 0); 
     displayMethod.setLayoutParams(params); 

     int i = 0;  
     while (i < 32){ 

      String x = method.getNextLine(); 

      displayMethod.append(x + "\n");    
      i++; 
     } 
     linLayout.addView(displayMethod); 
+0

那么你说当你旋转到风景你想有两个TextViews并排吗? (就像在一家报纸上将一个故事分成多列) – Tyler

+0

你可以通过在文件夹“layout-land”中定义另一个带有2列的xml布局文件来实现这一点 – user2340612

+0

@TylerAndFriends是的,现货!它需要通过编程方式生成,但并非全部在XML文件中完成 –

回答

2

有横向查看时将使用所有的屏幕空间的好方法,而且其实很简单拆分两TextView件的行为。

在您的res/layout文件夹旁边,创建一个名为layout-land的新布局文件夹,并在此处将您的新布局包含两个不同的TextView对象。请注意,layout-land中的新布局文件的名称需要与layout文件夹中的原始布局完全相同。 (实际上在这里复制粘贴)。

从这里,你有两个选择:

- 更新原有布局的layout文件夹中有垂直堆叠的两个TextView对象。确保TextViews的ID与layout-land中的横向布局相同。此选项不需要更改代码。

- 保持您的原始布局原样,但请检查Activity中是否存在风景特定的TextView,并适当填写一个或两个TextViews。例如...

TextView textView = findViewById(R.id.textView); 
TextView textViewLandscapeOnly = findViewById(R.id.textViewLandscapeOnly); 

if(textViewLandscapeOnly == null) { 
    //We're in portrait mode, so only fill the first text view with all 32 lines. 
} else { 
    //We're in landscape mode, so fill both text views, each with 16 lines. 
} 
+0

问题,我必须以编程方式生成TextView,原因是我无需解释。你的方法可以工作,但是它可以通过程序来完成,类似于我的代码片段 –