Android TextView Justify Text

前端 未结 29 1174
滥情空心
滥情空心 2020-11-22 03:28

How do you get the text of a TextView to be Justified (with text flush on the left- and right- hand sides)?

I found a possible solution here, but it do

相关标签:
29条回答
  • 2020-11-22 04:04

    While still not complete justified text, you can now balance line lengths using android:breakStrategy="balanced" from API 23 onwards

    http://developer.android.com/reference/android/widget/TextView.html#attr_android:breakStrategy

    0 讨论(0)
  • 2020-11-22 04:05

    You can use JustifiedTextView for Android project in github. this is a custom view that simulate justified text for you. It support Android 2.0+ and right to left languages. enter image description here

    0 讨论(0)
  • 2020-11-22 04:07

    Here's how I did it, I think the most elegant way I could. With this solution, the only things you need to do in your layouts are:

    • add an additional xmlns declaration
    • change your TextViews source text namespace from android to your new namespace
    • replace your TextViews with x.y.z.JustifiedTextView

    Here's the code. Works perfectly fine on my phones (Galaxy Nexus Android 4.0.2, Galaxy Teos Android 2.1). Feel free, of course, to replace my package name with yours.

    /assets/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);
        }
    }
    

    We need to set the rendering to software in order to get transparent background on Android 3+. Hence the try-catch for older versions of Android.

    Hope this helps!

    PS: please not that it might be useful to add this to your whole activity on Android 3+ in order to get the expected behavior:
    android:hardwareAccelerated="false"

    0 讨论(0)
  • 2020-11-22 04:08

    On android, to left justify text and not have truncation of the background color, try this, it worked for me, producing consistent results on android, ff, ie & chrome but you have to measure out the space that's left in between for the text when calculating the padding.

    <td style="font-family:Calibri,Arial;
        font-size:15px;
        font-weight:800;
        background-color:#f5d5fd;
        color:black;
        border-style:solid;
        border-width:1px;
        border-color:#bd07eb;
        padding-left:10px;
        padding-right:1000px;
        padding-top:3px;
        padding-bottom:3px;
    >
    

    The hack is the padding-right:1000px; that pushes the text to the extreme left.

    Any attempt to to a left or justify code in css or html results in a background that's only half width.

    0 讨论(0)
  • 2020-11-22 04:10

    I write a widget base on native textview to do it.

    github

    0 讨论(0)
  • 2020-11-22 04:10

    Android Text Justify For TextView XML

    Simply android text-justify using in XML. You can simply implement in textview widget.

     <TextView
        android:justificationMode="inter_word"
    />
    

    Default is android:justificationMode="none"

    0 讨论(0)
提交回复
热议问题