初始安卓基本控件_TextView

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() {...} 分别对应三个改变时期的方法,可以获取到原有字符串的字符个数等。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值