こんにちは、TextView を作成しようとしています。次の制約があります。
- 最大 2 行まで表示できます。2 行を超える場合は、末尾に「...」が表示されます。
- フォント サイズ 30 から始めます。まず、フォント サイズを 30 から 12 に減らして、すべてを 1 行に収めようとします。つまり、最初の行にフォント サイズ 20 ですべてを収めることができる場合は、フォント サイズ 20 のままにします。
- フォント サイズ 12 ですべてを収めることができない場合は、サイズ 12 のままにし、次の行に折り返し、すべてサイズ 12 のままにします。
これで、ユーザーがテキストを入力できるようになり、ユーザーが入力する各文字が TextView に反映され、上記のルールに従ってフォントサイズが変更されます。
userEditView.addTextChangedListener(
new TextWatcher() {
@Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
float fontSize = 30;
userTextView.setTextSize(fontSize);
int userTextViewWidth = userTextView.getWidth();
int userTextViewContainerWidth = parentRelatievLayOutView.getWidth();//parentRelativeLayout is a RelativeLayout in xml
// logic here => i want to know when to wrap a line, i should wrap when the textView width is same or greater than the parent container, in such case, we reduce the font size, and then get the new textView width, see if it can be fit in one line or not
while (userTextViewWidth >= userTextViewContainerWidth) {
fontSize -= 1;
if (fontSize <= 12) {
fontSize = 12;
break;
}
userTextView.setTextSize(fontSize);
//userTextView.append("\uFEFF"); // does not work
//userTextView.invalidate(); // does not work
userTextViewWidth = userTextView.getWidth();// *** this line never gets updated
}
}
@Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
userTextView.setText(charSequence);
}
@Override public void afterTextChanged(Editable editable) {
}
});
したがって、私の問題はuserTextViewWidth = userTextView.getWidth()
決して更新されません。つまり、フォントサイズが小さくなり、幅は同じです... TextViewのサイズが変更されないAndroidの問題があることを変更したいAndroid:TextViewの高さが縮小後に変更されないフォントサイズですが、試してみましたが、それが提供するテクニックはどれもうまくいきませんでした.