TextView控件和属性

一、显示文字的两种方法:

1.在src下的MainActivity.java中通过定义TestView对象实现(注意一定呀importTextView类)

TestView tv= new TestView(this);
tv.setText("你好");
setContentView(tv);

2.在布局中编辑,layout下的activity_main.xml中

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_text"
/>

其中,@string/hello-text,是指在string.xml文件中,找标签hello_text对应的内容:

<string name="hello_text">你好</string>


二、设置字体颜色的两种方法

1.在layout下的activity_main.xml中添加

android:textColor="#FF0000"

2.在 layout下的activity_main.xml中设置索引,然后借用索引值在在src下的MainActivity.java中设置新的文字与颜色

android:id="@+id/tv"

设置TextView对象名字tv,然后在 src下的MainActivity.java引用tv

TextView tv=(TextView)findViewById(R.id.tv);
tv.setText(Html.fromHtml("<font color=blue>欢迎你</font>"));

注意,这里使用了setText中,引用了Html,必须import Html的包才可以


三、TextView的常用属性

1.大小:android:textSize="30sp"【建议单位用sp】

2.宽度:android:layout_width="wrap_content"【建议单位用dp】

3.高度:android:layout_height="wrap_content"【建议单位用dp】

4.颜色:android:textColor="#FF0000"

5.超链:android:autoLink="all"【web/email/phone/map/all】

6.对象名:android:id="@+id/tv"

7.单行: android:singleLine="true"

8.跑马显示:android:ellipsize用于表示,当一行的文字过长时,怎样显示文字,start开头用省略号代替,end,结尾用省略号代替,middle中间用省略号代替,marquee已跑马灯形式显示。

其中,当设置android:ellipsize="marquee"时

需要设置,循环次数,android:marqueeRepeatLimit="marquee_forever"代表无限次循环

android:focusable="true"控件是否能够获得焦点

android:focusableInTouchMode="true"是否在触摸模式下获得焦点


    
    
    
   
    
    
    
    
    
    


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值