Подтвердить что ты не робот

TextView ломает мое слово буквами

Мои требования: создать "входящий пузырь" с шириной по содержанию и максимальной шириной 90%.

У меня есть эта разметка:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="1.0"
    tools:background="@color/white_smoke">

    <LinearLayout
        android:id="@+id/flBubble"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="start"
        android:background="@drawable/bubble_in"
        android:layout_weight="0.9">

        <ImageView
            android:id="@+id/ivSay"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="?android:attr/selectableItemBackground"
            android:contentDescription="@string/default_content_description"
            android:padding="8dp"
            android:src="@drawable/ic_play_circle_outline_black_24dp"
            android:tint="@color/primary"/>

        <TextView
            android:id="@+id/tvValue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:padding="8dp"
            android:textColor="@color/black"
            android:textSize="16sp"
            tools:text="I would like to go to an Italian restaurant"/>
    </LinearLayout>

    <View
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="0.1"/>
</LinearLayout>

Иногда я получаю следующий результат: плохая упаковка слов

Но я ожидаю следующий результат (это ложное поощрение скриншота из предварительного просмотра Android Studio): ожидаемая упаковка слов

Как я могу предотвратить разбиение слова restaraunt на буквы?

UPDATE

Хотя я использую minSdk = 15, я попытался использовать breakStrategy, и я не получил ожидаемого результата. android:breakStrategy="simple": простая стратегия разлома

android:breakStrategy="balanced": стратегия сбалансированного разрыва

Я нашел связанный с этим вопрос: Принудительное следующее слово к новой строке, если слово слишком длинное для текстового поля, но я не понял, как я могу получить максимальная доступная ширина для TextView с layout_width="wrap_content?

Было бы здорово, если бы я мог переопределить TextView.setText и поместить там разрывы строк там, где это необходимо.

4b9b3361

Ответ 1

OMG, в моей строке было &nbsp;!

value.replaceAll("\\s", " ");

Спасибо всем!

Ответ 2

Вы можете использовать webview для достижения такого поведения. В webview вы можете использовать css для настройки текста. Посмотрите этот ответ


Обновить

Вы можете рассчитать ширину строки и добавить \n в строку, где требуется разделение строки

Rect bounds = new Rect(); 
Paint textPaint = textView.getPaint(); 
 textPaint.getTextBounds(text, 0, text.length(), bounds); 
 int height = bounds.height(); 
int width = bounds.width();

Результаты отображаются в пикселях, поэтому просто проверьте ширину вашего view или экрана и разделите строку.


UPDAE2: Пример кода

Я просто написал пример с простой компоновкой в ​​действии onCreate, вы можете реализовать его в адаптере или что-то для вас.

    TextView textView = (TextView) findViewById(R.id.txt); //textview with empty text
    Rect bounds = new Rect();
    Paint textPaint = textView.getPaint();

    String text = "some long text here.....";// text data to work on
    textPaint.getTextBounds(text, 0, text.length(), bounds);
    int textWidth = bounds.width();// get text width in pixel
    int marginPadding = 100;// we have some padding and margin from xml layouts
    DisplayMetrics displayMetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    int rootWidth = displayMetrics.widthPixels-marginPadding;// maximum width on screan

    if (textWidth > rootWidth) { // check if need to split the string.
        int lineMax = (text.length() * rootWidth) / textWidth; // maximum Characters for each line
        String result = text.replaceAll("(.{" + String.valueOf(lineMax) + "})", "$1\n"); // regex to replace each group(lineMax) of Chars with group of char + new line
        textView.setText(result);
    } else
        textView.setText(text);

UPDATE # 3: Исправлен код для Listview

OnCreate

    ArrayList<String> data = new ArrayList<>();

    data.add("000");
    data.add("aaaaaaaaaaa");
    data.add("aaaaaaaaaaa bbbbbbbbbbbb");
    data.add("aaaaaaaaaaa bbbbbbbbbbbb cccccccccccccccc");
    data.add("aaaaaaaaaaa bbbbbbbbbbbb cccccccccccccccc ddddddddddddd");
    data.add("aaaaaaaaaaa bbbbbbbbbbbb cccccccccccccccc ddddddddddddd eeeeeeeeeeeee");
    data.add("aaaaaaaaaaa bbbbbbbbbbbb cccccccccccccccc ddddddddddddd eeeeeeeeeeeee ffffffffffffffffff");
    data.add("aaaaaaaaaaa bbbbbbbbbbbb cccccccccccccccc ddddddddddddd eeeeeeeeeeeee ffffffffffffffffff gggggggggggggggg");
    data.add("aaaaaaaaaaa bbbbbbbbbbbb cccccccccccccccc ddddddddddddd eeeeeeeeeeeee ffffffffffffffffff gggggggggggggggg hhhhhhhhhhhhhhhh");

    ListView listView = (ListView) findViewById(R.id.listview);
    MyAdapter adapter= new MyAdapter(data,this);
    listView.setAdapter(adapter);
    adapter.notifyDataSetChanged();

MyAdapter.java

public class MyAdapter extends BaseAdapter {

private LayoutInflater inflater = null;
Context context;
ArrayList<String> data;


public MyAdapter(ArrayList<String> data, Context context) {
    this.context = context;
    this.data = data;
    inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
    return data.size();
}

@Override
public Object getItem(int i) {
    return data.get(i);
}

@Override
public long getItemId(int i) {
    return i;
}


@Override
public View getView(final int i, View convertView, ViewGroup viewGroup) {

    final View view = inflater.inflate(R.layout.item, null);
    final TextView tv_text = (TextView) view.findViewById(R.id.tvValue);
    if (data.get(i) != null) {
        tv_text.post(new Runnable() {
            @Override
            public void run() {
               //TextView is Ready to be used.
                fixText(data.get(i),tv_text);
            }
        });
    }
    return view;
}

    private void fixText(String text, TextView textView) {
    Rect bounds = new Rect();
    Paint textPaint = textView.getPaint();
    textPaint.getTextBounds(text, 0, text.length(), bounds);
    int textWidth = bounds.width();// get text width in pixel
    int marginPadding = 100;// we have some padding and margin from xml layouts
    DisplayMetrics displayMetrics = new DisplayMetrics();
    ((MainActivity) context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    int rootWidth =  textView.getWidth();//displayMetrics.widthPixels - marginPadding;// maximum width on screan

    if (textWidth > rootWidth) { // check if need to split the string.
        //int lineMax = (text.length() * rootWidth) / textWidth; // maximum Characters for each line
        //String result = text.replaceAll("(.{" + String.valueOf(lineMax-5) + "})", "$1\n"); // regex to replace each group(lineMax) of Chars with group of char + new line
        String result = wrapText(rootWidth,text);
        textView.setText(result);
    } else
        textView.setText(text);



}

private String wrapText(int textviewWidth,String mQuestion) {
    String temp = "";
    String sentence = "";
    String[] array = mQuestion.split(" "); // split by space
    for (String word : array) {
        if ((temp.length() + word.length()) < textviewWidth) {  // create a temp variable and check if length with new word exceeds textview width.
            temp += " "+word;
        } else {
            sentence += temp+"\n"; // add new line character
            temp = word;
        }
    }
    return (sentence.replaceFirst(" ", "")+temp);
}

item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1.0"
tools:background="@color/colorAccent">

<LinearLayout
    android:id="@+id/flBubble"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_gravity="start"
    android:background="@color/colorPrimary"
    android:layout_weight="0.9">

    <ImageView
        android:id="@+id/ivSay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="?android:attr/selectableItemBackground"
        android:contentDescription="default_content_description"
        android:padding="8dp"
        android:src="@android:drawable/ic_media_play"
        android:tint="@color/colorPrimaryDark" />

    <TextView
        android:id="@+id/tvValue"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:padding="8dp"
        android:textColor="#000000"
        android:textSize="16sp"
        tools:text="I would like to go to an Italian restaurant jkjk l;'"/>
</LinearLayout>

<View
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_weight="0.1"/>
</LinearLayout>

Результат в устройстве

Ответ 3

Используйте свойство MaxWidth для текстового просмотра, иначе вы должны предоставить ширину для textview

   <com.custom.views.CustomTextView
        android:id="@+id/txt_send_chat"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:gravity="center_vertical"
        android:maxWidth="250dp"
        android:textColor="@color/color_chat_sender"
        android:textSize="16sp"
        app:font_name="@string/font_roboto_regular" />

Ответ 4

Попробуйте это

<TextView
        android:id="@+id/tvValue"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:padding="8dp"
        android:textColor="@color/black"
        android:textSize="16sp"
        tools:text="I would like to go to an Italian restaurant"/>
</LinearLayout>

Ответ 5

Вы можете попробовать Autosizing TextViews

Библиотека поддержки 26.0 обеспечивает полную поддержку функции авторазметки TextView на устройствах с версиями Android до Android 8.0 (уровень API 26). Библиотека поддерживает Android 4.0 (API уровня 14) и выше. Пакет android.support.v4.widget содержит класс TextViewCompat для доступа к функциям с обратной совместимостью.

Пример:

<TextView
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:autoSizeTextType="uniform" />

Подробнее... Руководства go ЗДЕСЬ

Их библиотека тоже ЗДЕСЬ

Ответ 6

Измените TextView на EditText и поместите две строки. он должен помочь вам

    android:inputType="textMultiLine"
    android:enabled="false"

Это правильно поместит текст, а позже вы сможете предоставить функцию редактирования в своем приложении, если вам нужно.

Ответ 7

Попробуйте это

 private String getWidthFitString(String input) {
    Paint paint = text.getPaint();
    // you can define max width by your self
    int maxWidth = getContentMaxWidth();
    float width = paint.measureText(input);
    if (width > maxWidth) {
        List<String> words = Arrays.asList(input.split("\\s"));
        int breakLinePosition = 0;
        String toBreakLineText;
        List<String> toBreakLineWords = new ArrayList<>();
        while (breakLinePosition < words.size()) {
            toBreakLineWords.add(words.get(breakLinePosition));
            toBreakLineText = TextUtils.join(" ", toBreakLineWords);
            float currentWidth = paint.measureText(toBreakLineText);
            if (currentWidth > maxWidth) {
                break;
            }
            breakLinePosition ++;
        }
        if (breakLinePosition > 1) {
            toBreakLineWords.remove(toBreakLineWords.size() - 1);
            toBreakLineText = TextUtils.join(" ", toBreakLineWords);
            List<String> fromBreakLineWords = new ArrayList<>();
            for (int i = breakLinePosition; i < words.size(); i++) {
                fromBreakLineWords.add(words.get(i));
            }
            return toBreakLineText + "\n" + getWidthFitString(TextUtils.join(" ", fromBreakLineWords));
        } else {
            return input;
        }
    }
    return input;
}

Ответ 8

Извините, не могу комментировать,

попробуйте следующее:

android:inputType="textMultiLine"