Android TextView Justify Text

前端 未结 29 1212
滥情空心
滥情空心 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: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:

    
    
        
            
        
    
    

    /res/layout/test.xml:

    
    
    
        
    
            
    
        
    
    

    /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", "
    "); loadDataWithBaseURL("file:///android_asset/", "" + "" + "" + text + "", "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"

提交回复
热议问题