TextView有这么几个属性,
TextView tv = null;
tv = (TextView) findViewById(R.id.tv);找控件
tv.setText(R.string.app_name);设置文本框内容,可设置资源id
tv.setTextSize(40);字体大小
tv.setTextColor(Color.RED);字体颜色
tv.setTypeface(Typeface.SANS_SERIF, Typeface.BOLD);字体样式
tv.setBackgroundColor(Color.BLUE);字体背景颜色
tv.setHeight(200);文本框高度
Toast.makeText(this, tv.getCurrentTextColor()+"", 0).show();当前字体颜色16进制数
Toast.makeText(this, tv.getText().toString(), 0).show();文本框内容
tv.setOnClickListener(new OnClickListener() {
点击事件
@Override
public void onClick(View v) {
}
});
tv.getWidth();获取宽度
tv.setPadding(10, 10, 10, 10);设置内边距
System.out.println("文本框左侧内边距为:"+tv.getPaddingLeft()获取左<span style="font-family: Arial, Helvetica, sans-serif;">内</span><span style="font-family: Arial, Helvetica, sans-serif;">边距值);</span>
下面是跑马灯的效果。
<TextView
android:layout_width="200px"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"
android:text="这是一个很长很长很长的文本"
android:textSize="20sp" />
还有可以选择字体textStyle属性,
autoLink是可以设置效果,比如点击打开网页;别的和View的基本属性差不多,就不一一介绍了。另外给textview设置监听最好不要采用xml写onClick方法,我测试过,好像不行,可以使用另外的方法。
补充TextView添加内容的一个方法,textview.append(String str,int start,int end);可以只要一个参数的,当使用三个参数时,后面2个参数指的是前面字符串的截取部分,并不是指在已有的textview上的长度。
TextView除了点击事件还有一个监听事件,textview.addTextChangedListener(new TextWatcher() {...} 分别对应三个改变时期的方法,可以获取到原有字符串的字符个数等。