简体   繁体   English

如果使用数据绑定表达式接收到值,如何在 Textview 的 2 行中排列文本

[英]How to arrange the text in 2 lines of Textview if it's value is received using databinding expression

As you can see in the code below I want the value in textview that has been set using databinding expression should be arranged in 2 lines after 22 digits in first line.正如您在下面的代码中所看到的,我希望使用数据绑定表达式设置的 textview 中的值应该在第一行的 22 位数字之后安排在 2 行中。 Please help me to figure out how it can be done.请帮我弄清楚如何完成。

<TextView
                android:id="@+id/cart_card_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="10dp"
                android:layout_marginTop="5dp"

                android:text="@{cartProductDetails.title}"
                android:maxLength="22"
                android:textSize="13sp"
                android:textStyle="bold"
                android:textColor="@color/black"
                app:layout_constraintStart_toEndOf="@id/cart_card_image"
                app:layout_constraintTop_toTopOf="parent" />

There are a few ways to achieve this behavior, depending on your specific requirements.有几种方法可以实现此行为,具体取决于您的具体要求。 Here's one approach that you could try:这是您可以尝试的一种方法:

Create a custom class that extends TextView and overrides the onMeasure() method, in order to insert line breaks into the text after 22 characters.创建一个扩展 TextView 并覆盖 onMeasure() 方法的自定义 class,以便在 22 个字符后的文本中插入换行符。

class WrappingTextView extends TextView {
public WrappingTextView(Context context) {
    super(context);
}

public WrappingTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public WrappingTextView(Context context, AttributeSet attrs, int defStyleAttr){
    super(context, attrs, defStyleAttr);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    String text = getText().toString();
    int textLength = text.length();
    if (textLength > 22) {
        String newText = text.substring(0, 22) + "\n" + text.substring(22);
        setText(newText);
    }
}
}

Change the TextView tag in your layout file to use your custom class and add the attribute android:inputType="textMultiLine" and android:maxLines="2"更改布局文件中的 TextView 标签以使用自定义 class 并添加属性 android:inputType="textMultiLine" 和 android:maxLines="2"

<com.example.WrappingTextView
android:id="@+id/cart_card_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="5dp"
android:text="@{cartProductDetails.title}"
android:maxLength="22"
android:inputType="textMultiLine"
android:maxLines="2"
android:textSize="13sp"
android:textStyle="bold"
android:textColor="@color/black"
app:layout_constraintStart_toEndOf="@id/cart_card_image"
app:layout_constraintTop_toTopOf="parent" />

This way it should wrap the text after 22 digits in the first line.这样它应该在第一行的 22 位数字之后换行。 Keep in mind that this code will only work if the textView width is not fixed and you expect the text to wrap.请记住,此代码仅在 textView 宽度不固定并且您希望文本换行时才有效。 If textView width is fixed you need to handle that with Ellipsize or something similar.如果 textView 宽度是固定的,你需要用 Ellipsize 或类似的东西来处理它。

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

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