简体   繁体   English

Android Studio:EditText 的 autoSize 不起作用

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

in my current project there's an EditText with fixed layout_width and layout_height, called exercise that is expanded downwards programmatically: One line of text (String) + "\n" is added to the EditText.在我当前的项目中,有一个具有固定 layout_width 和 layout_height 的 EditText,称为以编程方式向下扩展的exerciseOne line of text (String) + "\n"添加到 EditText。

Sometimes the line added to the EditText, let's call it element, is too long to fit inside the full width of the object so it's splitted into a new line.有时,添加到 EditText 的行(我们称之为元素)太长而无法容纳在对象的整个宽度内,因此将其拆分为新行。 The thing is I would either like the lines' text size in exercise to be resized to fit the EditText's width or have a clear visible distance between each line (element), but just not inside the newline due to not fitting inside the exercise 's width.问题是我希望在exercise中调整行的文本大小以适应 EditText 的宽度,或者在每行(元素)之间有清晰的可见距离,但由于不适合在exercise中,所以不在换行内宽度。

Therefore I googled as much as I could and tried out every possible solution I could find today.因此,我尽可能多地搜索并尝试了我今天能找到的所有可能的解决方案。 What I tried:我尝试了什么:

  • Using either EditText as the object and android:autoSizeTextType="uniform" & android:inputType="textMultiLine|textCapSentences" as attributes or androidx.appcompat.widget.AppCompatEditText , accompanied by the attributes app:autoSizeMaxTextSize="28sp" , app:autoSizeMinTextSize="8sp" and app:autoSizeStepGranularity="1sp" (with a device that supports just exactly API 26)使用EditText作为对象和android:autoSizeTextType="uniform" & android:inputType="textMultiLine|textCapSentences"作为属性或androidx.appcompat.widget.AppCompatEditText ,伴随着属性app:autoSizeMaxTextSize="28sp" , app:autoSizeMinTextSize="8sp"app:autoSizeStepGranularity="1sp" (使用仅支持 API 26 的设备)
  • using other types of text objects使用其他类型的文本对象
  • using lineSpacingExtra to insert some spacing.使用lineSpacingExtra插入一些间距。 This unfortunately also inserted the spacing between the wrapped/ splitted line so the original element's line that got splitted by wrapping inside the EditText had the spacing aswell.不幸的是,这也在包裹/分割线之间插入了间距,因此通过在 EditText 内包裹而被分割的原始元素的线也具有间距。

That's where I am now.这就是我现在的位置。 I can't get the text size be reduced automatically when the line would be too wide for the EditText's width.当行对于 EditText 的宽度来说太宽时,我无法自动减小文本大小。

I could supply the full XML, if needed.如果需要,我可以提供完整的 XML。

I'm grateful for any hint that could help here.我很感激任何可以在这里提供帮助的提示。 Thanks in advance!提前致谢!

you can try something like this你可以试试这样的

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

Here's a really basic RecyclerView implementation (using view binding, let me know if you're not familiar with that - you can just findViewById all the things instead):这是一个非常基本的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>

It's pretty simple - you have a text entry field and a button to add the contents as a new line.这很简单 - 您有一个文本输入字段和一个按钮,可以将内容添加为新行。 The button passes the contents to addItem on the adapter, which appends it to the list of lines in data .该按钮将内容传递给适配器上的addItem ,适配器将其附加到data中的行列表中。 The RecyclerView just displays all the items in data , using a ViewHolder layout that has an auto-sizing TextView to scale each item. RecyclerView仅显示data中的所有项目,使用具有自动调整大小的TextViewViewHolder布局来缩放每个项目。

Ideally you'd want to persist data somehow (eg the Add button passes the new data to a ViewModel , stores it somehow, updates the current list which the adapter has observe d so it updates whenever there's a change) - I just left it as a basic proof of concept.理想情况下,您希望以某种方式持久data (例如 Add 按钮将新数据传递给ViewModel ,以某种方式存储它,更新适配器observe的当前列表,以便在有变化时更新) - 我只是将其保留为概念的基本证明。 Also, it's easier to store separate items if they're kept separate - you can always serialise it by joining them into a single string if you really want!此外,如果将它们分开保存,则更容易存储单独的项目 - 如果您真的需要,您可以随时通过将它们连接成一个字符串来序列化它! But generally you wouldn't want to do that但通常你不想这样做

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM