android TextView 笔记

来源:互联网 发布:淘宝店一般能赚多少钱 编辑:程序博客网 时间:2024/05/01 05:04

1.当用TextView来响应事件时,需要双点才能响应:

certificates_tv_rec_date_start.setKeyListener(null);certificates_tv_rec_date_start.setOnClickListener(this);

即事情响应前添加----        
.setKeyListener(null);

2.textView添加删除线

public class HelloActivity extends Activity{   private  TextView tv;    @Override    public voidonCreate(Bundle savedInstanceState) {       super.onCreate(savedInstanceState);       setContentView(R.layout.main);              tv=(TextView)findViewById(R.id.tvId);       tv.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);    }}

其实Paint中的很多属性都可以用,比如下划线等等,大家可以自己测试下

3.TexTview单行多行

android:singleLine 用于指定该文本框是否为单行模式,其属性值为true或false,为true表示该文本框不会换行,当文本框中的文本超过一行时,其超出的部分将被省略,同时在结尾处添加“…”

android:ellipsize="end"android:singleLine="true"


设置行数

android:lines="3"

4.格式

android:autoLink 
用于指定是否将指定格式的文本转换为可单击的超级链接形式,其属性值有none、web、email、phone、map或all

5.跑马灯

XML实现跑马灯:

android:singleLine="true"     //只有单行才可能有跑马灯效果
android:ellipsize="marquee"    //跑马灯效果
android:marqueeRepeatLimit="marquee_forever"   //跑马灯重复次数        
android:focusable="true" //由于Activity中没有写任何方法,故此处让其获得焦点
android:focusableInTouchMode="true"

android:ellipsize="marquee"android:marqueeRepeatLimit="marquee_forever"android:singleLine="true"android:focusable="true"android:focusableInTouchMode="true"

继承TextView实现跑马灯

<TextView        android:id="@+id/marquee"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:background="#ffffff"        android:ellipsize="marquee"        android:focusable="true"        android:layout_gravity="top"        android:focusableInTouchMode="true"        android:marqueeRepeatLimit="marquee_forever"        android:scrollHorizontally="true"        android:singleLine="true"        android:textColor="#ff0000"        android:textSize="20dp" />

跑马灯封装

package com.syh.app.zsg.entity;import android.content.Context;import android.graphics.Rect;import android.util.AttributeSet;import android.widget.TextView;public class MarqueeText extends TextView {public MarqueeText(Context con) { super(con);}public MarqueeText(Context context, AttributeSet attrs) { super(context, attrs);}public MarqueeText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle);}@Overridepublic boolean isFocused() {return true;}@Overrideprotected void onFocusChanged(boolean focused, int direction,Rect previouslyFocusedRect) {// TODO Auto-generated method stub// super.onFocusChanged(focused, direction, previouslyFocusedRect);}}
引用
<com.syh.app.zsg.entity.MarqueeText                        android:id="@+id/wjl_weath_11"                        android:layout_marginTop="5dp"                        android:text="晴  3/13 周四"                        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:ellipsize="marquee"        android:focusable="true"        android:layout_gravity="top"        android:focusableInTouchMode="true"        android:marqueeRepeatLimit="marquee_forever"        android:scrollHorizontally="true"        android:singleLine="true" />

6.属性

在xml文件中使用android:textStyle=”bold” 可以将英文设置成粗体,但是不能将中文设置成粗体。
 将中文设置成粗体的方法是:

<pre name="code" class="java">TextView tv = (TextView)findViewById(R.id.TextView01);  TextPaint tp = tv.getPaint();  tp.setFakeBoldText(true); 

7.部分文字设置颜色

TextView tView1=(TextView) findViewById(R.id.tv3); //找到tv3组件tView1.setText(Html.fromHtml("大家好<font color=red>改变局部颜色</font>!")); //使用HTML标记设置文本

0 0
原创粉丝点击