控件:TextView

版权声明:本文为博主原创文章,码字不易,转载时请标明具体出处(博主名+博客页面链接),万分感谢Thanks♪(・ω・)ノ https://blog.csdn.net/An_nAl/article/details/78094430

纯粹记录一下自己常用的方法,防止万一哪天记不清楚的时候看看,会不断更新滴。。。

EditText控件 - CSDN博客 https://blog.csdn.net/An_nAl/article/details/79564788

文字一直滚动的TextView

xml布局文件:

 <TextView
                android:id="@+id/tv_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="10sp"
                android:padding="3dp"
                android:textColor="#29b6f6"/>               

修改显示的文字:

tvText.setText("abcde");                

修改显示的文字颜色:

tvText.setTextColor(Color.parseColor("#23A8e5"));

tvText.setTextColor(0xFF0000FF);  
//0xFF0000FF是int类型的数据,分组一下0x|FF|0000FF,0x是代表颜色整 数的标记,ff是表示透明度,0000FF表示颜色,注意:这里0xFF0000FF必须是8个的颜色表示,不接受0000FF这种6个的颜色表示。  
tvText.setTextColor(Color.rgb(255, 255, 255));  
tvText.setTextColor(Color.parseColor("#FFFFFF"));    
//还有就是使用资源文件进行设置  
tvText.setTextColor(this.getResources().getColor(R.color.blue));  
//通过获得资源文件进行设置。根据不同的情况R.color.blue也可以是R.string.blue或者  
//另外还可以使用系统自带的颜色类  
tvText.setTextColor(android.graphics.Color.BLUE);  

修改背景图片

 tvDataStatus.setBackgroundResource(R.mipmap.img);

行间距:

设置行间距,如”8dp”:

android:lineSpacingExtra 

设置行间距的倍数,如”1.5″:

android:lineSpacingMultiplier 

旋转45°的TextView

xml布局设置:

<TextView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:textSize="8sp"
   android:rotation="45"
   android:textColor="#ff"
   android:transformPivotY="11dp"
   android:transformPivotX="6dp"
   android:text="未通过"/>

自定义TextView控件:


public class RotateTextView extends TextView {
    
    public RotateTextView(Context context) {
        super(context);
    }

    public RotateTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //倾斜度45,上下左右居中
        canvas.rotate(45, getMeasuredWidth()/2, getMeasuredHeight()/2);
        super.onDraw(canvas);
    }

}

<com.xxx.RotateTextView
                   
                    
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:gravity="center"
                    android:paddingBottom="20dp"
                    android:textColor="@color/app_white"
                    android:textSize="12sp"
                   android:background="@mipmap/img"
                    android:text="未通过"/>

图片文字富文本显示:

出自:实战 | Android中文图混排时文图的居中对齐 FontMetrics以及自定义ImageSpan实现 https://mp.weixin.qq.com/s/2TTc-KucG6nH2upHqiZeOw

activity的xml布局:

<TextView
        android:id="@+id/text_view"
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:lineSpacingExtra="8dp"
        android:textSize="19sp"/>

activity的使用:

public class MainActivity extends AppCompatActivity {

    private TextView textView;

    //自定义对齐方式--与文字中间线对齐
    private final int  ALIGN_FONTCENTER = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = (TextView) this.findViewById(R.id.text_view);

        // "你们好,这个 是小Android!"
        // 这句话的空格位序为7,图片设置为铺盖此空格的位置;
        // 如果不加空格,则会覆盖“是”这个字!
        SpannableString spannableString = new SpannableString("你们好,这个 是小Android!");
        //调用自定义的imageSpan,实现文字与图片的横向居中对齐
        CustomImageSpan imageSpan = new CustomImageSpan(this, R.mipmap.ic_launcher, ALIGN_FONTCENTER);
        //setSpan插入内容的时候,起始位置不替换,会替换起始位置到终止位置间的内容,含终止位置。
        //Spanned.SPAN_EXCLUSIVE_EXCLUSIVE模式用来控制是否同步设置新插入的内容与start/end 位置的字体样式,此处没设置具体字体,所以可以随意设置
        spannableString.setSpan(imageSpan, 6, 7, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        textView.setText(spannableString);
    }
}

自定义imageSpan实现图片与文字的居中对齐:

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.annotation.DrawableRes;
import android.text.style.ImageSpan;

/**
 * 自定义imageSpan实现图片与文字的居中对齐
 */
class CustomImageSpan extends ImageSpan {

    //自定义对齐方式--与文字中间线对齐
    private int ALIGN_FONTCENTER = 2;

    public CustomImageSpan(Context context, int resourceId) {
        super(context, resourceId);
    }

    public CustomImageSpan(Context context, int resourceId, int verticalAlignment) {
        super(context, resourceId, verticalAlignment);
    }

    @Override
    public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom,
                     Paint paint) {

        // draw方法是重写的ImageSpan父类 DynamicDrawableSpan中的方法,在DynamicDrawableSpan类中,虽有getCachedDrawable(),
        // 但是私有的,不能被调用,所以调用ImageSpan中的getrawable()方法,该方法中会根据传入的drawable ID ,获取该id对应的
        // drawable的流对象,并最终获取drawable对象
        Drawable drawable = getDrawable(); //调用imageSpan中的方法获取drawable对象
        canvas.save();

        //获取画笔的文字绘制时的具体测量数据
        Paint.FontMetricsInt fm = paint.getFontMetricsInt();

        //系统原有方法,默认是Bottom模式
        int transY = bottom - drawable.getBounds().bottom;
        if (mVerticalAlignment == ALIGN_BASELINE) {
            transY -= fm.descent;
        } else if (mVerticalAlignment == ALIGN_FONTCENTER) {    //此处加入判断, 如果是自定义的居中对齐
            //与文字的中间线对齐(这种方式不论是否设置行间距都能保障文字的中间线和图片的中间线是对齐的)
            // y+ascent得到文字内容的顶部坐标,y+descent得到文字的底部坐标,(顶部坐标+底部坐标)/2=文字内容中间线坐标
            transY = ((y + fm.descent) + (y + fm.ascent)) / 2 - drawable.getBounds().bottom / 2;
        }

        canvas.translate(x, transY);
        drawable.draw(canvas);
        canvas.restore();
    }

    /**
     * 重写getSize方法,只有重写该方法后,才能保证不论是图片大于文字还是文字大于图片,都能实现中间对齐
     */
    public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
        Drawable d = getDrawable();
        Rect rect = d.getBounds();
        if (fm != null) {
            Paint.FontMetricsInt fmPaint = paint.getFontMetricsInt();
            int fontHeight = fmPaint.bottom - fmPaint.top;
            int drHeight = rect.bottom - rect.top;

            int top = drHeight / 2 - fontHeight / 4;
            int bottom = drHeight / 2 + fontHeight / 4;

            fm.ascent = -bottom;
            fm.top = -bottom;
            fm.bottom = top;
            fm.descent = top;
        }
        return rect.right;
    }
}

值得参考的网址:
1.TextView属性大全 - 寒星晓月 专注移动互联网 - 博客园 http://www.cnblogs.com/hxxy2003/archive/2011/08/05/2129050.html
2.TextView实战之你真的懂我么? - PleaseCallMeCoder - CSDN博客 http://blog.csdn.net/sdkfjksf/article/details/51317204这一篇讲得很全,常使用得方法都讲了
3.这篇的文字图片富文本讲解得非常棒:自定义Span - 简书 https://www.jianshu.com/p/deb28c22852a

猜你喜欢

转载自blog.csdn.net/An_nAl/article/details/78094430