Android中的TextView

本文将介绍Android中TextView及相关属性

1、TextView

行间距:
lineSpacingExtra
lineSpacingMultiplier

<TextView
android:lineSpacingExtra="10dp"
android:layout_marginTop="10dp"
android:text="@string/iAmTextview"
ndroid:background="#f00"        android:layout_width="wrap_content"        android:layout_height="wrap_content" />
<TextView
android:lineSpacingMultiplier="2"
android:layout_marginTop="10dp"
android:text="@string/iAmTextview"
android:background="#f00"
android:layout_width="wrap_content"        android:layout_height="wrap_content" />

单行显示
singleLine

<TextView
android:singleLine="true"
android:layout_marginTop="10dp"
android:text="@string/iAmTextview"
android:background="#0f0"        android:layout_width="wrap_content"        android:layout_height="wrap_content" />

单行显示,且中间以…显示
singleLine
ellipsize

 <TextView
android:singleLine="true"
android:ellipsize="middle"
android:layout_marginTop="10dp"
android:text="@string/iAmTextview"
android:background="#00f"        android:layout_width="wrap_content"        android:layout_height="wrap_content" />

注:要设置为跑马灯形式且循环,设置ellipsize为marquee,:focusable为true,focusableInTouchMode为true,且代码设置该控件为requestfocus

最多显示几行

 <TextView
        android:maxLines="2"
        android:layout_marginTop="10dp"
        android:text="@string/iAmTextview"
        android:background="#f00"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

设置字体

<TextView
android:fontFamily="monospace"
android:lines="2"
android:layout_marginTop="10dp"
android:text="@string/iAmTextview"
android:background="#0f0"            android:layout_width="wrap_content"        android:layout_height="wrap_content" />

从assets加载字体

TextView textView = (TextView) findViewById(R.id.textview1);
Typeface typeFace = Typeface.createFromAsset(getAssets(), "font.ttf");
textView.setTypeface(typeFace);

给TextView设置图标

<TextView
android:padding="30dp"
android:drawablePadding="20dp"        
android:drawableLeft="@drawable/logo_mobile"        android:drawableTop="@drawable/logo_mobile"        android:drawableRight="@drawable/logo_mobile"        android:drawableBottom="@drawable/logo_mobile"        android:text="@string/iAmTextview1"
android:gravity="right"
android:background="#0f0"
android:layout_marginTop="20dp"
android:layout_width="300dp"
android:layout_height="230dp" />

TextView的文本混排即Span的使用

TextView textView = (TextView) findViewById(R.id.textview1);

SpannableString ss = new SpannableString(getResources().getString(R.string.iAmTextview));
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.logo_mobile);
//34 这个位置加入图片
ss.setSpan(new ImageSpan(this, bitmap), 3, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//815有下划线
ss.setSpan(new UnderlineSpan(), 8, 15, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//设置前景色
ss.setSpan(new ForegroundColorSpan(Color.WHITE), 20, 25, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//设置背景色
ss.setSpan(new BackgroundColorSpan(Color.WHITE), 27, 29, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//设置点击事件
ss.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                Toast.makeText(TextViewActivity.this, "clicked", Toast.LENGTH_LONG).show();
            }
        }, 30, 38, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//设置字体大小
        ss.setSpan(new AbsoluteSizeSpan(30, true), 42, 46, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//设置字体大小
        ss.setSpan(new RelativeSizeSpan(1.5f), 48, 50, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//设置字体样式,(斜体,粗体)
        ss.setSpan(new StyleSpan(Typeface.BOLD | Typeface.ITALIC), 52, 56, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//文字间加一横线
        ss.setSpan(new StrikethroughSpan(), 58, 60, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        ss.setSpan(new StrikethroughSpan(), 58, 60, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//设置字体
        ss.setSpan(new TypefaceSpan("monospace"), 62, 65, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//设置一个样式
        ss.setSpan(new TextAppearanceSpan(this, R.style.myTextAppearance), 67, 70, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//设置自己的一个样式
        ss.setSpan(new MySpan(Color.YELLOW), 71, 75, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//点击时间起作用必须加的
        textView.setMovementMethod(LinkMovementMethod.getInstance());
//
        textView.setText(ss);

设置AutoLink

<TextView
android:autoLink="all"
android:textColorLink="#000"
android:id="@+id/textview1"        android:text="@string/iAmTextview3"
android:background="#f00"
android:textColor="#00f"        android:layout_width="match_parent"        android:layout_height="wrap_content" />

注:autolink有其他属性,all,email,phone,web

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值