所以我来到了一个适合我的解决方案。它需要获取屏幕宽度,计算TextView的宽度并检查文本长度,以及dp中的所有内容。所以:
// get the screen width
var metrics = Resources.DisplayMetrics;
var widthInDp = (int)((metrics.WidthPixels)/metrics.Density);
// this line is very specific, it calculates the real usable space
// in my case, there was padding of 5dp nine times, so subtract it
var space = widthInDp - 9 * 5;
// and in this usable space, I had 7 identical TextViews, so a limit for one is:
var limit = space/days.Length;
// now calculating the text length in dp
Paint paint = new Paint();
paint.TextSize = myTextView.TextSize;
var textLength = (int)Math.Ceiling(paint.MeasureText(myTextView.Text, 0, myTextView.Text.Length)/metrics.Density);
// and finally formating based of if the text fits (again, specific)
if (textLength > limit)
{
myTextView.Text = myTextView.Text.Substring(0, myTextView.Text.IndexOf("-"));
}
现在看起来很简单,但我只是把它留在这里,也许有人会觉得它有用。
你可以添加一个侦听器到'textview'的'addTextChangedListener'和'afterTextChanged'中,计数'\ n'字符吗? –
也许你可以用它来检查你的视图才出现https://developer.android.com/reference/android/view/ViewTreeObserver.OnGlobalLayoutListener.html –
GlobalLayout事件让我觉得我从来没有看到“长“文本了,但布局仍然闪烁着双线。 TextChanged事件没有\ n字符,并且LineCount当然是0。 –