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

1、TextView

行间距:
lineSpacingExtra
lineSpacingMultiplier

android:lineSpacingExtra="10dp"android:layout_marginTop="10dp"android:text="@string/iAmTextview"ndroid:background="#f00"        android:layout_width="wrap_content"        android:layout_height="wrap_content" />
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

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

 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" />

设置字体

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设置图标

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

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

更多相关文章

  1. android中涉及的字体修改
  2. android AlertDialog中EditText无法显示软键盘问题的解决方案
  3. [置顶] 自定义漂亮的Android(安卓)SeekBar样式
  4. android的ListView做表格添加圆角边框
  5. Android(安卓)采用工厂类创建对话框
  6. android 修改标题样式以及内容
  7. Android(安卓)字体自适应设置
  8. 自定义Android的Spinner
  9. 知识点整理(三)易错记录

随机推荐

  1. Android 8.1 关机充电动画(一)模式选择
  2. Android使用Handler实时更新UI
  3. 浅谈android的selector 背景选择器
  4. x86平台编译Android
  5. Android(安卓)Call Log and SMS Delete
  6. android实现富文本
  7. Android中的数据绑定框架DataBinding(对
  8. Android 特殊界面效果之——透明界面
  9. Android(安卓)-- 保存文件
  10. 计算器——第一个Android小项目