【发布时间】:2016-03-28 11:16:07
【问题描述】:
我想在TextView 中使用两端对齐的文本格式,所以我创建了class JustifiedTextView extends WebView。
但我无法设置外部Typeface。
我在我的代码中尝试了setTypeface,它没有给出任何错误,但Typeface 没有在TextView 中设置。
我的班级中与Typeface 相关的任何更改?
.XML
<com.example.Model.JustifiedTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@id/article_decription" />
JustifiedTextView.Java
public class JustifiedTextView extends WebView {
private String core = "<html><body style='text-align:justify;color:rgba(%s);font-size:%dpx;margin: 10px 10px 10px 10px;'>%s</body></html>";
private String textColor = "0,0,0,255";
private String text = "";
String fontBody = "fonts/AvenirLTStd-Book.otf";
Typeface typeFace = Typeface.createFromAsset(getContext().getAssets(), fontBody);
private int textSize = 18;
private int backgroundColor = Color.TRANSPARENT;
public JustifiedTextView(Context context, AttributeSet attrs){
super(context, attrs);
this.setWebChromeClient(new WebChromeClient() {
});
}
public void setText(String s) {
this.text = s;
// this.setPadding(10, 10, 10, 10);
reloadData();
}
@SuppressLint("NewApi")
private void reloadData() {
// loadData(...) has a bug showing utf-8 correctly. That's why we need
// to set it first.
// this.getSettings().setDefaultTextEncodingName("utf-8");
this.loadData(String.format(core, textColor, textSize, text, typeFace), "text/html", "utf-8");
// set WebView's background color *after* data was loaded.
super.setBackgroundColor(backgroundColor);
// Hardware rendering breaks background color to work as expected.
// Need to use software renderer in that case.
if (android.os.Build.VERSION.SDK_INT >= 11)
this.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);
}
public void setTextColor(int hex) {
String h = Integer.toHexString(hex);
int a = Integer.parseInt(h.substring(0, 2), 16);
int r = Integer.parseInt(h.substring(2, 4), 16);
int g = Integer.parseInt(h.substring(4, 6), 16);
int b = Integer.parseInt(h.substring(6, 8), 16);
textColor = String.format("%d,%d,%d,%d", r, g, b, a);
reloadData();
}
public void setBackgroundColor(int hex) {
backgroundColor = hex;
reloadData();
}
public void setTextSize(int textSize) {
this.textSize = textSize;
reloadData();
}
public void setTypeFace(Typeface typeFace){
this.typeFace=typeFace;
reloadData();
}
}
MainActivity.Java
Typeface typeFontBody = Typeface.createFromAsset(getAssets(), fontBody);
JustifiedTextView Description;
Description = (JustifiedTextView)itemView.findViewById(R.id.article_decription);
Description.setTypeFace(typeFontBody);
Description.setText("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book");
【问题讨论】:
-
有一个用于证明 textview 的库 使用它可以帮助我没有链接,但是该库的这个 gradle 行
compile 'com.github.bluejamesbond:textjustify-android:2.1.1' -
@VivekMishra 你是否正确设置了
fonts目录 -
是的,我的代码与该库完美配合
-
#Viveka Patel
public class JustifiedTextView extends TextView -
将评论标记为有帮助,因为您发布了自己的答案