XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
android:id="@+id/test1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:singleLine="false"
    />
    <TextView
    android:id="@+id/test2"  
    android:singleLine="false"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    />
</LinearLayout>
.java    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);         TextView t1 = (TextView)findViewById(R.id.test1);
         TextView t2 = (TextView)findViewById(R.id.test2);
        
         String s1 = "1111111111";
         String s2 = "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB";
    
        t1.setText(s1);
        t2.setText(s2);        int nCount1 = t1.getLineCount();
        int nCount2 = t2.getLineCount();        Log.e("test", "nCount1="+nCount1);
        Log.e("test", "nCount2="+nCount2);以上代码显示出来三个textview分别为,一,二行。但是为何打印出来的log三个全是0?
如果我要获取该textview中的文本行数,该怎么办?
请高手指点!

解决方案 »

  1.   

    你加个button,点击后打印log看看
    public int getLineCount ()Since: API Level 1
    Return the number of lines of text, or 0 if the internal Layout has not been built.
    sdk里说如果layout没有建立起来就返回0
      

  2.   

    这个涉及到Activity的生命周期的问题。
    程序启动时想要获取视特定视图组件的尺寸大小,在onCreate中可能无法取到,因为窗口Window对象还没创建完成。