Android学习笔记---第二天---基础UI组件---TextView
TextView用于显示文本的组件,我们可以在XML中设置它的属性也可以在Java代码中动态的设置它。
常用属性
android:layout_width //设置组件宽,在之前使用过;所有组件都有的属性
android:layout_height //设置组件宽,在之前使用过;所有组件都有的属性
android:id //设置组件ID如@+id/newTextView;格式为@+id/(id名);所有组件都有的属性
android:text //文本内容
android:textSize //字体大小单位为sp
android:layout_gravity //该组件在父组件中的位置,之前使用过;所有组件都有的属性
android:gravity //该组件中内容的位置,之前使用过;所有组件都有的属性
android:background//设置文本背景色
android:textColor //设置字体颜色
android:textStyle//设置字体风格加粗与倾斜
android:autoLink //自动文本,设置文本是否可以被单击的超链接需指定链接类型如web,email等
android:textIsSelectable //设置文本是否可以被选择
android:textColorLink//设置链接文本的颜色
代码
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/newTextView0" android:text="www.csdn.net" android:textSize="30sp" android:layout_gravity="center" android:gravity="center" android:background="#dcadad" android:textColor="#b1b33b" android:textStyle="bold|italic" android:autoLink="web" android:textIsSelectable="true"/>
效果图
单击文本时会使用浏览器打开CSDN官网。
相关方法
(突然想起来我好想之前一直说函数来着。。。)
其实这个没什么好说的设置属性的方法名只用在刚刚说的名前面加上set就好了;
如android:text就是textView.setText("这是修改后的内容");这样textView这个对象的的文本内容就被修改了;
获取textView的文本内容就是textView.getText();
代码
TextView textView = (TextView)findViewById(R.id.newTextView0); //通过id找到我们在xml文件里写TextView这个函数返回一个View对象;; try { textView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 0)); //设置android:layout_height与android:width属性的值; textView.setText("这是改变后的TextView".toCharArray(),0,"这是改变后的TextView".toCharArray().length); //设置android:text属性的值; textView.setTextColor(Color.BLUE);//设置字体颜色,这里使用了Color类中的颜色常量; textView.setBackgroundColor(Color.rgb(254, 123, 111));//设置android:background属性值; //这里使用了Color类中的rgb方法三个参数分别三原色的值,还有argb方法相对rgb增加一个alpha值; textView.setTextSize(20);//设置字体大小 } catch (NullPointerException e) { e.printStackTrace(); }我将这段代码写在了onCreate方法中;
运行效果图
其他
TextView是EditText和Button的父类并且直接继承View类。
它还派生了TextlClock和Chronometer以及CheckedTextView三个类。
更多相关文章
- Android(安卓)TextView文字横向自动滚动(跑马灯)
- TextView设置跑马灯的样式
- 《Android/OPhone 开发完全讲义》已出版,现提供源代码下载
- Android(安卓)ScrollView去掉滚动条及ScrollView属性
- Android(安卓)APN设置
- Android(安卓)Studio主题设置
- android 控制屏幕亮度
- 【Android】实现动态显示隐藏密码输入框的内容
- Android应用程序四大组件