2

こんにちは、TextView を作成しようとしています。次の制約があります。

  1. 最大 2 行まで表示できます。2 行を超える場合は、末尾に「...」が表示されます。
  2. フォント サイズ 30 から始めます。まず、フォント サイズを 30 から 12 に減らして、すべてを 1 行に収めようとします。つまり、最初の行にフォント サイズ 20 ですべてを収めることができる場合は、フォント サイズ 20 のままにします。
  3. フォント サイズ 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の高さが縮小後に変更されないフォントサイズですが、試してみましたが、それが提供するテクニックはどれもうまくいきませんでした.

4

2 に答える 2

3

あなたがする必要があるのは、あなたのtextViewを測定することです。

それ以外の

userTextViewWidth = userTextView.getWidth();

使用する

// find out how wide it 'wants' to be    
userTextView.measure(MeasureSpec.UNSPECIFIED, userTextView.getHeight()); 
userTextViewWidth = userTextView.getMeasuredWidth();

詳細はhttp://developer.android.com/reference/android/view/View.html#Layoutにあります

于 2012-11-14T17:53:28.177 に答える
0

を設定android:layoutWidth="wrap_content"すると、textview はその中のテキストの長さに基づいて幅のサイズをスケーリングします。私の知る限り、テキストサイズに基づいてテキストビューのサイズを自動変更する方法はありません。

于 2012-10-23T02:12:03.077 に答える