TextView通过字母打破了我的话

我的要求:创build宽度为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"平衡的休息策略

我发现一个相关的问题: 强制下一个单词到一个新的行,如果单词太长 ,但我没有undestand如何获得最大的可用宽度的TextView与layout_width="wrap_content

如果我可以覆盖TextView.setText并在需要的地方放置换行符,那将会很棒。

Solutions Collecting From Web of "TextView通过字母打破了我的话"

你可以使用webview来实现这种行为。 在web视图中,你可以使用CSS来调整文本。 看看这个答案


更新

您可以计算string的宽度,并将\n添加到string需要分割的string中

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

结果以像素为单位,因此只需检查view或屏幕的宽度并拆分string即可。


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); 

更新#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> 

导致设备

为textview使用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" /> 

尝试这个

 <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> 

您可以尝试使用自动调整TextViews

支持库26.0提供对运行Android 8.0(API级别26)之前的Android版本的设备上的自动调整TextViewfunction的全面支持。 该库提供对Android 4.0(API级别14)及更高版本的支持。 android.support.v4.widget包中包含TextViewCompat类,以向下兼容的方式访问function

例如:

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

有关更多详细信息指南请点击此处

他们在这里也是图书馆

改变你的TextView到EditText并把这两行。 它应该帮助你

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

这将正确地放置文本,稍后如果需要,您可以在应用程序中提供编辑function。

OMG,那里有&nbsp; 在我的string!

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

谢谢你们!

抱歉不能评论,

尝试这个:

 android:inputType="textMultiLine"