TextView控件
该控件较为常见,可显示文本,在文本上实现一些效果。
1、实现字体的大小,颜色的改变。
<TextView
android:id="@+id/tv_1" //定义名字
android:layout_width="wrap_content" //自动宽度
android:layout_height="wrap_content" //自动高度
android:text="薛定谔的12138" //显示文字,“ ”内为显示的内容
android:textColor="#66ccff" //文本颜色
android:textSize="30sp" //字体大小,常用sp作为单位
android:paddingLeft="15dp"/> //该控件距左边的距离
效果展示
2、当控件长度不足以显示文本时显示 “…”
<TextView
android:id="@+id/tv_2"
android:layout_width="80dp" //定义一个宽度
android:layout_height="wrap_content"
android:maxLines="1" //这里必须定义最大行数,否则一行显示不下会默认换行
android:ellipsize="end" //该语句实现文本后面的"..."效果
android:text="薛定谔的12138"
android:textColor="#66ccff"
android:textSize="30sp"
android:layout_marginTop="10dp" //距上一个控件的距离
android:paddingLeft="15dp"/>
效果展示
3、文字后跟图片类型(如下拉菜单的样式)
<TextView
android:id="@+id/tv_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="薛定谔的类型"
android:drawableRight="@drawable/arrow_down" //这里为自己找的箭头图片,将其放到drawable文件夹下引用即可
android:drawablePadding="5dp" //图片与文本的距离
android:textSize="30sp"
android:textColor="#000000"
android:layout_marginTop="10dp"
android:paddingLeft="15dp"
/>
效果展示
需注意:放置在drawable文件夹下的图片最好建一个独立的子文件夹,我是自己建了一个drawable-xxhdpi文件夹专门存放图片。
4、文字中划线
在.xml文件里
<TextView
android:id="@+id/tv_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="你好啊"
android:textSize="30sp"
android:textColor="#000000"
android:layout_marginTop="10dp"
android:paddingLeft="15dp"
/>
在.java文件里
public class TextViewActivity extends AppCompatActivity {
private TextView mTv4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text_view);
mTv4 = (TextView) findViewById(R.id.tv_4);
mTv4.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG); //中划线
mTv4.getPaint().setAntiAlias(true); //去除锯齿
}
}
效果展示
5、文字下划线
在.xml文件里
<TextView
android:id="@+id/tv_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="薛定谔的12138"
android:textColor="#66ccff"
android:textSize="30sp"
android:layout_marginTop="10dp"
android:paddingLeft="15dp"/>
在.java文件里
public class TextViewActivity extends AppCompatActivity {
private TextView mTv5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mTv5 = (TextView)findViewById(R.id.tv_5);
mTv5.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线
}
}
效果展示
6、文字跑马灯
在许多界面里过多的文字需要在同一行里轮播,就会需要跑马灯效果。
在.xml文件里
<TextView
android:id="@+id/tv_7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="薛定谔薛定谔薛定谔你好你好再见再见再见明天见"
android:textSize="30sp"
android:textColor="#000000"
android:layout_marginTop="10dp"
android:singleLine="true" //单行显示
android:ellipsize="marquee" //跑马灯的效果
android:marqueeRepeatLimit="marquee_forever" //循环的次数 forever即永远循环
android:focusable="true" //有焦点的情况下可实现跑马灯
android:focusableInTouchMode="true"
android:paddingLeft="15dp"/>
效果展示
以上即为较常用的TextView的控件属性。