簡體   English   中英

Android Studio:EditText 的 autoSize 不起作用

[英]Android Studio: autoSize of EditText doesn't work

在我當前的項目中,有一個具有固定 layout_width 和 layout_height 的 EditText,稱為以編程方式向下擴展的exerciseOne line of text (String) + "\n"添加到 EditText。

有時,添加到 EditText 的行(我們稱之為元素)太長而無法容納在對象的整個寬度內,因此將其拆分為新行。 問題是我希望在exercise中調整行的文本大小以適應 EditText 的寬度,或者在每行(元素)之間有清晰的可見距離,但由於不適合在exercise中,所以不在換行內寬度。

因此,我盡可能多地搜索並嘗試了我今天能找到的所有可能的解決方案。 我嘗試了什么:

  • 使用EditText作為對象和android:autoSizeTextType="uniform" & android:inputType="textMultiLine|textCapSentences"作為屬性或androidx.appcompat.widget.AppCompatEditText ,伴隨着屬性app:autoSizeMaxTextSize="28sp" , app:autoSizeMinTextSize="8sp"app:autoSizeStepGranularity="1sp" (使用僅支持 API 26 的設備)
  • 使用其他類型的文本對象
  • 使用lineSpacingExtra插入一些間距。 不幸的是,這也在包裹/分割線之間插入了間距,因此通過在 EditText 內包裹而被分割的原始元素的線也具有間距。

這就是我現在的位置。 當行對於 EditText 的寬度來說太寬時,我無法自動減小文本大小。

如果需要,我可以提供完整的 XML。

我很感激任何可以在這里提供幫助的提示。 提前致謝!

你可以試試這樣的

if(et.getText().length()>10) {
        et.setTextSize(newValue)

這是一個非常基本的RecyclerView實現(使用視圖綁定,如果您不熟悉,請告訴我 - 您可以改為findViewById所有東西):

結果的圖片,有多條不同大小的線

class MainFragment : Fragment(R.layout.fragment_main) {

    lateinit var binding: FragmentMainBinding

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding = FragmentMainBinding.bind(view)
        with(binding) {
            val adapter = MyAdapter()
            recyclerView.adapter = adapter
            recyclerView.layoutManager = LinearLayoutManager(requireContext())

            addButton.setOnClickListener {
                val item = textEntry.text.toString()
                if (item.isNotBlank()) {
                    adapter.addItem(item)
                    textEntry.text.clear()
                }
            }
        }
    }


}

class MyAdapter : RecyclerView.Adapter<MyAdapter.ViewHolder>() {

    private var data: List<String> = emptyList()

    fun addItem(item: String) {
        data = data + item
        notifyItemInserted(data.lastIndex)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = ItemViewBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.binding.textView.text = data[position]
    }

    override fun getItemCount(): Int = data.size

    class ViewHolder(val binding: ItemViewBinding) : RecyclerView.ViewHolder(binding.root)
}
fragment_main.xml


<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:gravity="center"
    android:padding="10dp">

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_marginBottom="8dp"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toTopOf="@id/textEntry"
    />

    <EditText
        android:id="@+id/textEntry"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:singleLine="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="@id/addButton"
        />

    <Button
        android:id="@+id/addButton"
        android:text="ADD"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        />


</androidx.constraintlayout.widget.ConstraintLayout>
item_view.xml


<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingHorizontal="16dp"
    >
    <TextView
        android:id="@+id/textView"
        app:autoSizeTextType="uniform"
        android:layout_width="match_parent"
        android:layout_height="48sp"
        android:maxLines="1"
        android:gravity="center_vertical"
        />

</FrameLayout>

這很簡單 - 您有一個文本輸入字段和一個按鈕,可以將內容添加為新行。 該按鈕將內容傳遞給適配器上的addItem ,適配器將其附加到data中的行列表中。 RecyclerView僅顯示data中的所有項目,使用具有自動調整大小的TextViewViewHolder布局來縮放每個項目。

理想情況下,您希望以某種方式持久data (例如 Add 按鈕將新數據傳遞給ViewModel ,以某種方式存儲它,更新適配器observe的當前列表,以便在有變化時更新) - 我只是將其保留為概念的基本證明。 此外,如果將它們分開保存,則更容易存儲單獨的項目 - 如果您真的需要,您可以隨時通過將它們連接成一個字符串來序列化它! 但通常你不想這樣做

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM