gpt4 book ai didi

android - 如何以编程方式检查字符串是否太宽以适应 Android 中的屏幕

转载 作者:行者123 更新时间:2023-11-29 16:06:09 25 4
gpt4 key购买 nike

我有一个自定义的 ListView。在自定义 ListView 中,我有一个 TextView,它看起来像这样

enter image description here

现在,我的问题是

1) 有什么方法可以检查名称是否太宽以适应 Android 屏幕?我的意思是我不想要两条线。像这里一样 Wachler 在第二行。那么,如何检查所有名称是否都足够宽以在一行中显示在屏幕上。

最佳答案

//1) 
layout_width = yourLayout.getWidth(); //in order to measure the width of the ListView rows container

查找文字大小:

//2)
Paint paint = new Paint();
Rect bounds = new Rect();

int text_height = 0;
int text_width = 0;

paint.setTypeface(Typeface.DEFAULT);// your preference here
paint.setTextSize(25);// have this the same as your text size

String text = "Some random text";

paint.getTextBounds(text, 0, text.length(), bounds);

text_height = bounds.height();
text_width = bounds.width();

//3)

if(text_width < layout_width){
//..do your magic
}

Source

关于android - 如何以编程方式检查字符串是否太宽以适应 Android 中的屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18180799/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com