你如何得到一个TextView的文本是正当的(与文本flush在左边和右手边)?
我在这里找到了一个可能的解决方案,但它不起作用(即使您将vertical-center更改为center_vertical等)。
你如何得到一个TextView的文本是正当的(与文本flush在左边和右手边)?
我在这里找到了一个可能的解决方案,但它不起作用(即使您将vertical-center更改为center_vertical等)。
当前回答
我是这么做的,我认为是我能做到的最优雅的方式。有了这个解决方案,你只需要在你的布局中做的事情是:
添加一个额外的XMLNS声明 改变你的TextViews源文本命名空间从android到你的新命名空间 用x.y.z.JustifiedTextView替换你的textview
这是代码。在我的手机上运行得非常好(Galaxy Nexus Android 4.0.2, Galaxy Teos Android 2.1)。当然,您可以随意用您的包名替换我的包名。
/资产/ justified_textview.css:
body {
font-size: 1.0em;
color: rgb(180,180,180);
text-align: justify;
}
@media screen and (-webkit-device-pixel-ratio: 1.5) {
/* CSS for high-density screens */
body {
font-size: 1.05em;
}
}
@media screen and (-webkit-device-pixel-ratio: 2.0) {
/* CSS for extra high-density screens */
body {
font-size: 1.1em;
}
}
/res/values/attrs.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="JustifiedTextView">
<attr name="text" format="reference" />
</declare-styleable>
</resources>
res / layout / test.xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res/net.bicou.myapp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<net.bicou.myapp.widget.JustifiedTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
myapp:text="@string/surv1_1" />
</LinearLayout>
</ScrollView>
/src/net/bicou/myapp/widget/JustifiedTextView.java:
package net.bicou.myapp.widget;
import net.bicou.myapp.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.webkit.WebView;
public class JustifiedTextView extends WebView {
public JustifiedTextView(final Context context) {
this(context, null, 0);
}
public JustifiedTextView(final Context context, final AttributeSet attrs) {
this(context, attrs, 0);
}
public JustifiedTextView(final Context context, final AttributeSet attrs, final int defStyle) {
super(context, attrs, defStyle);
if (attrs != null) {
final TypedValue tv = new TypedValue();
final TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.JustifiedTextView, defStyle, 0);
if (ta != null) {
ta.getValue(R.styleable.JustifiedTextView_text, tv);
if (tv.resourceId > 0) {
final String text = context.getString(tv.resourceId).replace("\n", "<br />");
loadDataWithBaseURL("file:///android_asset/",
"<html><head>" +
"<link rel=\"stylesheet\" type=\"text/css\" href=\"justified_textview.css\" />" +
"</head><body>" + text + "</body></html>",
"text/html", "UTF8", null);
setTransparentBackground();
}
}
}
}
public void setTransparentBackground() {
try {
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
} catch (final NoSuchMethodError e) {
}
setBackgroundColor(Color.TRANSPARENT);
setBackgroundDrawable(null);
setBackgroundResource(0);
}
}
我们需要将渲染设置为软件,以便在Android 3+上获得透明的背景。因此,老版本的Android会出现“试接”现象。
希望这能有所帮助!
PS:为了得到预期的行为,请不要将此添加到Android 3+上的整个活动中: android: hardwareAccelerated = " false "
其他回答
@CommonsWare的答案是正确的。Android 8.0+确实支持“完全论证”(或简单的“论证”,因为它有时被含糊地提到)。
Android还支持“左/右文本对齐”。请参阅维基百科关于区分理由的文章。许多人认为“对齐”的概念包括完全对齐以及左/右文本对齐,这是他们在想要进行左/右文本对齐时最终搜索的内容。这个答案解释了如何实现左/右文本对齐。
可以实现左/右文本对齐(而不是问题所询问的完全对齐)。为了演示,我将使用一个基本的2列表单(标签在左列,文本字段在右列)作为示例。在本例中,左列标签中的文本将右对齐,因此它们将与右列中的文本字段齐平。
在XML布局中,你可以得到的TextView元素本身(左列)对齐到右边,通过添加以下属性在所有的TextView:
<TextView
...
android:layout_gravity="center_vertical|end">
...
</TextView>
但是,如果文本换行到多行,文本仍将在TextView内保持左对齐。添加以下属性使实际文本在TextView内右对齐(不规则左对齐):
<TextView
...
android:gravity="end">
...
</TextView>
重力属性指定如何对齐TextView内的文本layout_gravity指定如何对齐/布局TextView元素本身。
更新
我们为此创建了一个简单的类。目前有两种方法可以达到你想要的效果。两者都不需要WEBVIEW和支持SPANNABLES。
图书馆:https://github.com/bluejamesbond/TextJustify-Android
支持:Android 2.0到5。X
设置
// Please visit Github for latest setup instructions.
截图
我不认为Android支持完全的理由。
更新2018-01-01:Android 8.0+支持TextView对齐模式。
虽然仍然不是完整的文本,你现在可以平衡行长度使用android:breakStrategy="balanced"从API 23起
http://developer.android.com/reference/android/widget/TextView.html#attr_android:breakStrategy
请在8.0以下尝试此代码
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center|start"/>