TextView
介绍
用于在界面上显示一段文本信息,以下是它的5种功能:
文字大小、颜色
<TextView
android:id="@+id/text_view1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="24sp"
android:textColor="#000000"
android:text="Hello World!" />
在values/strings.xml下可以设置文本要输出的内容:
<resources>
<string name="app_name">Cat</string>
<string name="tv_test1">Hello World!</string>
</resources>
再修改如下代码:
android:text="@string/tv_test1"
文字大小单位用sp是因为:碎片化的问题
显示不下使用...
<TextView
android:id="@+id/text_view2"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:maxLines="1"
android:ellipsize="end"
android:textSize="24sp"
android:textColor="#000000"
android:text="@string/tv_test1" />
文字+icon
<TextView
android:id="@+id/text_view3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@string/tv_test1"
android:drawableRight="@drawable/bg"
android:textColor="#000000"
android:textSize="24sp" />
中划线、下划线
<TextView
android:id="@+id/text_view4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@string/tv_test1"
android:textColor="#000000"
android:textSize="24sp" />
在MainActivity的onCreate()方法中添加如下代码:
TextView textView4 = (TextView) findViewById(R.id.text_view4);
//中划线
textView4.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
//去锯齿
textView4.getPaint().setAntiAlias(true);
//下划线
//textView4.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
//或textView4.setText(Html.fromHtml("<u>天哥在奔跑</u>"));
跑马灯
<TextView
android:id="@+id/text_view5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World! Hello World! Hello World! Hello World! Hello World!"
android:textColor="#000000"
android:textSize="24sp"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
android:clickable="true"/>
注意:在真机中要跑起来需要加最后一行android:clickable="true"代码,在模拟机中跑的话不需要,效果如下:在真机中点击一下第5行就能跑起来了