how to use getlinecount() in textview android

2019-02-16 18:11发布

i want to know how many line in my text view. i already set mytextview text, then i want to get how many line it take in mytextview.

i use mytextview.getLineCount() but it didn't work. it always return 0.

can someone helpme.

6条回答
smile是对你的礼貌
2楼-- · 2019-02-16 18:36

You need to post method for fetching the lines counts. Here is the sample code

imageCaption.setText("Text Here");
imageCaption.post(new Runnable() {

    @Override
    public void run() {

        int lineCount    = imageCaption.getLineCount();

        Log.v("LINE_NUMBERS", lineCount+"");
    }
});
查看更多
欢心
3楼-- · 2019-02-16 18:38

you can check the TextView parameters inside onCreateOptionsMenu()

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    TextView tv1 = (TextView)findViewById(R.id.textView);
    Rect rect = new Rect();
    int c = tv1.getLineCount();
    tv1.getLineBounds(0, rect);
    tv1.getLineBounds(1, rect);
    return true;
}
查看更多
啃猪蹄的小仙女
4楼-- · 2019-02-16 18:38

thanks for all. i solved it already. i use thread to get the linecount just like this

@Override
public void run() {
    // TODO Auto-generated method stub
    while(textView.getLineCount() == 0){

    }
    countLine = textView.getLineCount(); 

}

hope this will help if you have same problem. best regard.

查看更多
兄弟一词,经得起流年.
5楼-- · 2019-02-16 18:48

The Following will provide the lines count of the textview at the same place you set tv.setText().

int maxTextViewWidth="ENTER MAX WIDTH HERE";

tv.setText("hello\nhow are you?");

int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(maxTextViewWidth, View.MeasureSpec.AT_MOST);

int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);

tv.measure(widthMeasureSpec, heightMeasureSpec);

int lineCount=tv.getLineCount();
查看更多
你好瞎i
6楼-- · 2019-02-16 18:50

public int getLineCount ()

Since: API Level 1

Return the number of lines of text, or returns 0 if the internal Layout has not been built.

查看更多
相关推荐>>
7楼-- · 2019-02-16 18:51

Please check this link.

Android Edittext: Get LineCount in an Activity's onCreate()

tv.getLineCount(), will always retirn 0, if internal layout is not created yet.

查看更多
登录 后发表回答