文章目录
参考书籍:第一行代码:Android(第二版)(郭霖):第三章
一、常用控件使用方法
1、初学暂时使用的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
2、TextView
TextView可以说是Android中最简单的一个控件,它主要用于在界面上显示一段文本信息,比如第一章中的HelloWorld,更多的使用方法如下
<!--
android:id:该组件的唯一标识符
android:layout_width:宽
android:layout_height:高
三个值可选:match_parent(当前控件的大小和父布局一样)、fill_parent(意思同上)、wrap_content(当前控件的大小刚好包含住里面的内容)
android:text:指定在TextView中显示的文本内容(文字默认是居左上角对齐)
android:gravity:指定组件内文字的对齐方式,可选top\bottom\left\right\center等
可以用"|"来同时指定多个值,center的效果等同于center_vertical|center_horizontal(竖直和水平都居中)
android:textSize:设置文字的大小(sp为单位)
android:textColor:设置文字颜色
android:textStyle:设置字体风格:italic(斜体),bold(加粗),normal(默认)
android:background:设置控件的背景颜色
android:shadowColor:设置阴影颜色,需要与shadowRadius一起使用
android:shadowDx:设置阴影再说水平方向的偏移,就是水平方向阴影开始的横坐标位置
android:shadowDy:设置阴影再说竖直方向的偏移,就是水平方向阴影开始的纵坐标位置
android:shadowRadius:设置阴影的模糊程序,设为0.1就变成字体颜色,建议使用3.0
跑马灯如下:
android:singleLine:内容单行显示
android:focusable:是否可以获取焦点
android:focusableInTouchMode:用于控制视图在触摸模式下是否可以聚焦
android:ellipsize:在哪里省略文本(marquee滚动、start省略号在前面、end省略号在后面、middle省略号在中间、none)
android:marqueeRepeatLimit:字幕动画重复的次数marquee_forever无限次数
以下代码实现跑马灯
-->
<TextView
android:id="@+id/text_view"
android:layout_width="match_parent"
android:layout_height="100dp"
android:gravity="center"
android:text="HelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorld"
android:background="#BBC8C3"
android:textStyle="italic"
android:textSize="24sp"
android:textColor="#229000FF"
android:shadowColor="@color/purple_700"
android:shadowDx="15.0"
android:shadowDy="15.0"
android:shadowRadius="10.0"
android:singleLine="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:ellipsize="marquee"