2017-11-01 18 views
3

최근에 안드로이드는보기 크기와 최소 및 최대 텍스트 크기에 따라 TextViews 텍스트 크기의 크기 조정을 지원합니다.
https://developer.android.com/guide/topics/ui/look-and-feel/autosizing-textview.htmlEditText 자동 자르기

불행히도, 그들은 EditText를 지원하지 않으므로 EditText를위한 다른 대안이 있습니까?

+0

https://github.com/ViksaaSkool/AutoFitEditText을하는 데 도움이

희망이 모든 물건을 피할 수 있을까? –

답변

0

EditText가 TextView의 자식이지만 어떻게 자동 크기 조절을 지원하지 않습니까?

나는 이것을 해킹으로 해결했습니다. 처음에는 EditTextView에서 (Kotlin의) Extension으로 복사하고 구현할 TextView 코드를 보았습니다. 그러나 메소드가 너무 많아서 결국에는 그 옵션을 무시합니다. 내가 무엇을

, 그것은 눈에 보이지 않는 텍스트 뷰를 사용하는의 (예 나는 완전한 해킹을 알고 매우 행복하지 오전하지만 구글은 이것에 대해 부끄러워해야)

이 내 XMLS입니다

<TextView android:id="@+id/invisibleTextView" 
    android:layout_height="0dp" 
    android:layout_width="match_parent" 
    android:focusable="false" 
    app:autoSizeTextType="uniform" 
    app:autoSizeMinTextSize="@dimen/text_min" 
    app:autoSizeMaxTextSize="@dimen/text_max" 
    app:autoSizeStepGranularity="@dimen/text_step" 
    android:textAlignment="center" 
    app:layout_constraintLeft_toLeftOf="@id/main" 
    app:layout_constraintRight_toRightOf="@id/main" 
    app:layout_constraintTop_toBottomOf="@id/textCount" 
    app:layout_constraintBottom_toBottomOf="@id/main" 
    android:visibility="invisible" 
    tool:text="This is a Resizable Textview" /> 


<EditText android:id="@+id/resizableEditText" 
    android:layout_height="0dp" 
    android:layout_width="match_parent" 
    android:textAlignment="center" 
    app:layout_constraintLeft_toLeftOf="@id/main" 
    app:layout_constraintRight_toRightOf="@id/main" 
    app:layout_constraintTop_toBottomOf="@id/textCount" 
    app:layout_constraintBottom_toBottomOf="@id/main" 
    android:maxLength="@integer/max_text_length" 
    tool:text="This is a Resizable EditTextView" /> 

: 그것은 모두보기

그런 다음 내 코드에 내 EditTextView에 사용하려면이 텍스트 뷰에서 autocalculations를 사용하는 것과 동일한 너비/높이가 중요합니다.

private fun setupAutoresize() { 
    invisibleTextView.setText("a", TextView.BufferType.EDITABLE) //calculate the right size for one character 
    resizableEditText.textSize = autosizeText(invisibleTextView.textSize) 
    resizableEditText.setHint(R.string.text_hint) 

    resizableEditText.addTextChangedListener(object : TextWatcher { 
     override fun afterTextChanged(editable: Editable?) { 
      resizableEditText.textSize = autosizeText(invisibleTextView.textSize) 
     } 

     override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {} 

     override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) { 
      textCount.text = currentCharacters.toString() 
      val text = if (s?.isEmpty() ?: true) getString(R.string.text_hint) else s.toString() 
      invisibleTextView.setText(text, TextView.BufferType.EDITABLE) 
     } 
    }) 
} 

private fun autosizeText(size: Float): Float { 
    return size/(resources.displayMetrics.density + MARGIN_FACTOR /*0.2f*/) 
} 

참고로, 힌트의 크기를 변경하려면이 Android EditText Hint Size을 사용하십시오.

확실한 해결 방법은 있지만 적어도 추후 버전에서 크기를 변경할 때조차도 계속 작동 할 것이라고 확신합니다. 사기성 또는 버려진 github lib는 실패 할 것입니다. 나는 언젠가 희망

, 구글은 우리를 듣고 차일에이 멋지 기능을 구현, 우리는이