答案 0 :(得分:1)
我也一直试图使它起作用,我无法使其自动变为椭圆大小(或MARQUEE fwiw)。我将分享我作为变通办法所做的事情,但可能与您所寻找的不完全相同(也不完全是我想要的)。我基本上以固定的数字创建自己的椭圆尺寸(我使用25,它适用于我测试过的小屏幕)。 sa_displayedNames
是填充数字选择器的内容。
String[] sa_displayedNames =addEllipsize(sa_displayedNames);
public String[] addEllipsize(String[] sa){
for(int i = 0; i < sa.length; i++){
sa[i] =ellipsize(sa[i], 25);
}
return sa;
}
public String ellipsize(String input, int maxLength) {
String s_ellipse = "...";
if (input ==null || input.length() <= maxLength || input.length() < s_ellipse.length()) return input;
return input.substring(0, maxLength - s_ellipse.length()).concat(s_ellipse);
}
答案 1 :(得分:0)
通过寻求“静止”的答案来满足我的需求。
NumberPicker numberPicker;
String[] strings;
EditText editText = numberPicker.findViewById(
Resources.getSystem().getIdentifier("numberpicker_input", "id", "android"));
numberPicker.setDisplayedValues(ellipsizeStringArray(
strings,
editText.getPaint(),
numberPicker.getWidth()));
@NonNull
private String[] ellipsizeStringArray(@NonNull String[] displayedValues,
@NonNull TextPaint textPaint,
int viewWidth) {
if (viewWidth == 0) {
return displayedValues;
}
for (int i = 0; i < displayedValues.length; i++) {
int textLength = displayedValues[i].length();
if (displayedValues[i] == null || textLength == 0) {
continue;
}
int textWidth = Math.round(textPaint.measureText(displayedValues[i]));
if (textWidth <= viewWidth) {
continue;
}
int charLength = textWidth / textLength;
int maxLength = viewWidth / charLength;
displayedValues[i] = ellipsize(displayedValues[i], maxLength);
}
return displayedValues;
}
@NonNull
private String ellipsize(@NonNull String input, int maxLength) {
String ellipse = "...";
if (input.length() <= maxLength
|| input.length() < ellipse.length()
|| maxLength <= ellipse.length) {
return input;
}
return input.substring(0, maxLength - ellipse.length()).concat(ellipse);
}