Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
字体大小更改后,Android TextView未调整大小_Android_Textview_Android Edittext - Fatal编程技术网

字体大小更改后,Android TextView未调整大小

字体大小更改后,Android TextView未调整大小,android,textview,android-edittext,Android,Textview,Android Edittext,您好,我正在尝试使用TextView,它有以下限制: 它最多可以走2行,如果超过2行,它只会在末尾显示“…” 从字体大小30开始,我们首先尝试通过将字体大小从30减小到12来将所有内容放在一行中。这意味着,如果我们可以在第一行中使用20号字体,我们就坚持使用20号字体 如果我们不能用12号字体填充所有内容,我们将保持12号,然后换行到下一行,所有内容都将保持12号 现在我有了一个EditText,它允许用户键入文本,用户输入的每个字符,TextView将反映用户键入的内容,并根据上面的规则更改字

您好,我正在尝试使用TextView,它有以下限制:

  • 它最多可以走2行,如果超过2行,它只会在末尾显示“…”
  • 从字体大小30开始,我们首先尝试通过将字体大小从30减小到12来将所有内容放在一行中。这意味着,如果我们可以在第一行中使用20号字体,我们就坚持使用20号字体
  • 如果我们不能用12号字体填充所有内容,我们将保持12号,然后换行到下一行,所有内容都将保持12号
  • 现在我有了一个EditText,它允许用户键入文本,用户输入的每个字符,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) {
      }
    });
    
    userEditView.addTextChangedListener(
    新的TextWatcher(){
    @重写更改前的公共void(CharSequence CharSequence,int i,int i1,int i2){
    浮点数=30;
    userTextView.setTextSize(fontSize);
    int userTextViewWidth=userTextView.getWidth();
    int userTextViewContainerWidth=parentRelatievLayOutView.getWidth();//parentRelativeLayout是xml中的RelativeLayout
    //logic here=>我想知道何时换行,当textView宽度等于或大于父容器时,我应该换行,在这种情况下,我们减小字体大小,然后得到新的textView宽度,看看它是否适合一行
    while(userTextViewWidth>=userTextViewContainerWidth){
    字体大小-=1;
    
    如果(fontSizeSet
    android:layoutWidth=“wrap_content”
    ,textview将根据其中的文本长度缩放宽度大小。好的,没有办法根据textsize自动调整textview的大小。

    您需要做的是测量您的textview

    而不是

    userTextViewWidth = userTextView.getWidth();
    
    使用


    更多信息位于

    抱歉,您的解决方案不适合我的情况。我有一些限制,我需要在一个有2行的文本视图中尽可能多地容纳字符。是的,android中可能没有直接api可以帮助我,这就是我尝试使用宽度的原因。
    // find out how wide it 'wants' to be    
    userTextView.measure(MeasureSpec.UNSPECIFIED, userTextView.getHeight()); 
    userTextViewWidth = userTextView.getMeasuredWidth();