Android TextView换行行为(如何分割最后一个单词) [英] Android textview break line behaviour (how to split the last word)
问题描述
我正在写一个应用程序,该应用程序会在屏幕上弹出文本,通常长于1行.如果一行的最后一个单词太长而无法容纳,则textview会将其放在下一行的开头. 有没有办法修改这种行为?如果最后一个单词很长,结果将是可怕的.
I'm writing an apply that pops texts on the screen and usually it is longer than 1 line. If the last word of a line is too long to fit in it, textview would just put it at the beginning of the next line. Is there a way to modify this behaviour? When the last word is a long one the results are horrible.
基本上我想实现这样的目标
Basically I'd like to achieve something like this
| Lorem ipsum dolor坐在,安全|
| tur adipiscing精英. Lorem ipsum dol |
|或坐着,方便贴身 |
|精简. nbsp; bsp b |
|Lorem ipsum dolor sit amet, consecte|
|tur adipiscing elit. Lorem ipsum dol |
|or sit amet, consectetur adipiscing |
|elit. |
代替我现在得到的东西.
Instead of what I'm getting now.
| Lorem ipsum dolor sit amet, |
| consectetur精英人士.洛雷姆|
| ipsum dolor坐着,安全|
|忠实的精英. nbsp; bsp |
|Lorem ipsum dolor sit amet, |
|consectetur adipiscing elit. Lorem |
|ipsum dolor sit amet, consectetur |
| adipiscing elit. |
谢谢!
其他信息
我有一个textview,它显示具有不同长度的随机报价.我需要的是一种分割每行最后一个单词的方法.
I have this textview that displays random quotes that have different lengths. What I need is a way to split the last word of every line.
我正在寻找一种类似此!
这是布局xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/title" />
<TextView
android:id="@+id/quote"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/quote" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="refresh"
android:text="@string/tellmemore" />
推荐答案
我决定切换到WebView,因为使用HTML,我可以使用理由.您可以阅读 和这是一个很好的教程.
如果您正在寻找抵押,那么您可以查看 Google的hypenator.js
I decided to switch to a WebView because with HTML I'd have access to justification. You can read about it here and here and if you want to inject css this is a good tutorial.
If you are looking for hypenation maybe you can look into the Google's hypenator.js
所以,在这里回答自己就是解决方案!
So, to answer to myself here is the solution!
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFD0" >
<WebView
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:onClick="refresh"
android:text="@string/tellmemore" />
和活动
...
WebView mWebView;
@Override
protected void onCreate(Bundle savedIstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedIstanceState);
setContentView(R.layout.webview);
mWebView = (WebView) findViewById(R.id.webview);
load();}
public void load() {
// some other code to parse the string from a database
String text = "<html> <head>" + tokens[1].toString()"</head><body>" + "<p align=\"justify\">"+ tokens[0].toString() + "</p> " + "</body></html>";
mWebView.loadData(text, "text/html", "utf-8");}
这篇关于Android TextView换行行为(如何分割最后一个单词)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!