更改对齐的TextView的字体

维维卡·帕特尔(Viveka Patel)

我想在其中使用对齐文本格式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");
维维卡·帕特尔(Viveka Patel)

Android文本全断言/包装/对齐/连字符-V2.0 https://github.com/bluejamesbond/TextJustify-Android

建立等级

dependencies {

    compile 'com.github.bluejamesbond:textjustify-android:2.1.1'
}

XML文件

 <com.bluejamesbond.text.DocumentView xmlns:ext="http://schemas.android.com/apk/res-auto"
                                         android:layout_width="fill_parent"
                                         android:layout_height="fill_parent"
                                         ext:documentView_antialias="true"
                                         ext:documentView_cacheConfig="auto_quality"
                                         ext:documentView_hyphen="-"
                                         ext:documentView_insetPadding="10dp"
                                         ext:documentView_insetPaddingBottom="10dp"
                                         ext:documentView_insetPaddingLeft="10dp"
                                         ext:documentView_insetPaddingRight="10dp"
                                         ext:documentView_insetPaddingTop="10dp"
                                         ext:documentView_lineHeightMultiplier="2.0"
                                         ext:documentView_maxLines="100"
                                         ext:documentView_offsetX="10dp"
                                         ext:documentView_offsetY="10dp"
                                         ext:documentView_progressBar="@id/progressBarXml"
                                         ext:documentView_reverse="false"
                                         ext:documentView_text="@string/xml_test_data"
                                         ext:documentView_textAlignment="justified"
                                         ext:documentView_textColor="@android:color/white"
                                         ext:documentView_textFormat="plain"
                                         ext:documentView_textSize="12sp"
                                         ext:documentView_textStyle="bold|strikeThru|underline"
                                         ext:documentView_textSubPixel="true"
                                         ext:documentView_textTypefacePath="fonts/helvetica.ttf"
                                         ext:documentView_wordSpacingMultiplier="5.0"/>

MainActivity.Java

DocumentView Description;
Description = (DocumentView) itemView.findViewById(R.id.article_decription);
Description.setText("TEXT");

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

以编程方式更改TextView对齐

来自分类Dev

根据设备Swift的方向更改对象的字体,样式等

来自分类Dev

GridBagLayout:添加复选框后更改对齐方式

来自分类Dev

如何防止系统字体大小更改对Android应用程序的影响?

来自分类Dev

R对功能-如何更改对角线值的字体大小?

来自分类Dev

如何防止系统字体大小更改对android应用程序的影响?

来自分类Dev

是否不针对mfc中的Windows dpi更改对话框的字体?

来自分类Dev

将元素添加到居中元素的侧面,而无需更改对齐方式

来自分类Dev

更改字体大小并对齐导航栏品牌

来自分类Dev

更改对象的值

来自分类Dev

更改对象的属性

来自分类Dev

更改对象的值

来自分类Dev

更改对象格式

来自分类Dev

更改对象的属性

来自分类Dev

如何在不更改对齐的情况下使用linux命令将终端的输出写入新文件?

来自分类Dev

如何在android textview中进行文本对齐,同时使用自定义字体样式?

来自分类Dev

更改使用自定义字体的textview部分的字体颜色

来自分类Dev

当我们更改Samsung设备中的设备字体样式时,如何防止TextView字体更改

来自分类Dev

通过编程更改了text属性后,在iOS 7的IB中配置的textView将字体更改回默认字体

来自分类Dev

更改对象的嵌套属性

来自分类Dev

更改对象从NSUserDefaults获取

来自分类Dev

jQuery更改对象的值

来自分类Dev

jQuery更改对象的值

来自分类Dev

动态更改对象属性

来自分类Dev

WPF更改对象的位置

来自分类Dev

更改对象(如果存在)

来自分类Dev

Javascript更改对象值

来自分类Dev

更改对象封装的数组

来自分类Dev

PInvoke不会更改对象

Related 相关文章

热门标签

归档