Android基础(一)基础控件

1.TextView:用来显示字符串的组件,在手机上就是显示一块文本的区域。

<TextView
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:text="Hello World! 你好,世界!Hello World! 你好,世界!Hello World! 你好,世界!"
        android:textSize="20sp"
        android:textColor="#f00"
        android:hint="这是提示语(text文本为空或没有的时候提示语才显示)"
        android:singleLine="true"
        android:background="#5500ffee"
        android:inputType="textPassword"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

知识点:
(1)表示宽度和高度的三种方式:
①match_parent 全部占满 ②wrap_content 自适应 ③100dp 具体的数值
(2)android:background="#5500ffee" 前两位表示透明度,后六位表示RGB颜色;背景也可以设置为图片
(3)android:singleLine=“true” 设置TextView显示一行,未显示部分打点(…)表示
(4)android:lines=“3” 设置TextView的行数
拓展:android:lines=“1”
android:ellipsize=“end”
以上两句可以实现与 android:singleLine=“true” 一样的效果
(5)android:inputType=“textPassword” 设置TextView为密码显示方式,将以点的形式代表字符
还有一种过时的表示方式:android:password=“true”
(6)android:text=“www.baidu.com”
android:autoLink=“web”
以上两句可以实现链接,点击可跳转
拓展:
在TextView 中设置autoLink 属性可以自动识别Web URL、电话号码、电子邮件地址、添加下划线改变字体颜色并实现点击事件,支持自动识别的类型:
android:autoLink=“web” 匹配Web URL。
android:autoLink=“phone” 匹配电话号码
android:autoLink=“email” 匹配电子邮件地址
android:autoLink=“map” 匹配地理位置
android:autoLink=“all” 匹配所有可用的模式
android:autoLink=“none” 不匹配任何类型
也可以类似这样的设置 android:autoLink=“web|phone” 表示匹配WebURL和手机号

2.EditText(输入框):是TextView的子类,它继承了TextView的所有属性。

<EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入用户名"
        android:textColorHint="#ccc"
        android:text="xiaoan"
        android:textSize="30sp"
        android:textColor="#f40"
        android:textStyle="bold"
        android:singleLine="true"
        android:maxLength="10" />

知识点:
(1)android:background="@null" 去掉下划线
android:background="#00f" 给背景设置颜色(也可以设置为图片)
(2)android:digits=“1234567890” 设置只接收指定的文本内容(这里设置为只能输入数字0-9)
(3)android:inputType=“textPassword” 设置文本的类型,用于帮助输入法显示合适的键盘类型(这里设置为以密码形式显示输入的文本)
(4)android:maxLength=“10” 设置EditText最多接受的文本的个数
(5)android:textAllCaps=“true” 默认text文本大写(存在 android:singleLine=“true” 时失效;设置了之后默认文本不能删除,也不能再输入文本)

3.Button

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮"
        android:textColor="@color/black"
        android:background="@color/cardview_shadow_start_color"
        android:clickable="false" />

知识点:
(1)android:background="@color/cardview_shadow_start_color" (setBackgroundResource(int resid) -> 通过资源文件设置背景色(resid:资源xml文件ID;按钮默认背景为android.R.drawable.btn_default)
(2)android:clickable=“false” 设置是否允许点击,默认为true(允许点击)
(3)android:textAllCaps=“false” 在默认情况下,按钮的名称无论有大小写字母,都只显示出大写字母,将textAllCaps属性设置为false可以显示出大小写字母
(4)新版Android Studio给Button设置背景颜色的坑:如果在xml中给控件设置了android:background属性,设置无效(Button仍然是默认颜色,默认颜色是标题栏的颜色),而且在Activity.java文件中设置背景颜色也无效。只有在xml中未给控件设置android:background属性而在Activity.java文件中设置背景颜色才生效。Activity.java文件中设置背景颜色的三种方式:
①button_background.setBackgroundColor(getResources().getColor(R.color.purple_200));
②button_background.setBackgroundColor(Color.rgb(255, 0, 0));
③button_background.setBackgroundColor(Color.parseColor("#6600ff00"));

4.ImageView

<ImageView
        android:id="@+id/iv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@mipmap/p1"
        android:scaleType="center"
        android:background="#fcc"/>

知识点:
scaleType缩放类型设置:
①fitXY:对图像的横向与纵向进行独立缩放,使得该图片完全适应ImageView,但是图片的横纵比可能会发生改变
②fitStart:保持纵横比缩放图片,知道较长的边与Image的编程相等,缩放完成后将图片放在ImageView的左上角
③fitCenter:同上,缩放后放于中间;
④fitEnd:同上,缩放后放于右下角;
⑤center:保持原图的大小,显示在ImageView的中心。当原图的size大于ImageView的size,超过部分裁剪处理。
⑥centerCrop:保持横纵比缩放图片,知道完全覆盖ImageView,可能会出现图片的显示不完全
⑦centerInside:保持横纵比缩放图片,直到ImageView能够完全地显示图片
⑧matrix:默认值,不改变原图的大小,从ImageView的左上角开始绘制原图,原图超过ImageView的部分作裁剪处理

5.SeekBar

<TextView
        android:id="@+id/tv_progress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="60" />
<SeekBar
        android:id="@+id/seek_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:progress="60"
        android:max="200"
        android:secondaryProgress="160" />

知识点:
(1)android:progress=“60” 进度条默认值
(2)android:max=“200” 进度条最大值
(3)android:secondaryProgress=“160” “缓冲”

6.RatingBar

<TextView
        android:id="@+id/tv_rating"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0"
        android:textSize="25sp" />
<RatingBar
        android:id="@+id/rating_bar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:numStars="5"
        android:rating="3.5"
        android:stepSize="0.5"
        android:isIndicator="false"
        style="?attr/ratingBarStyle" />

知识点:
(1)android:numStars=“5” 星星的总数量
(2)android:rating=“3.5” 设置默认评分数
(3)android:stepSize=“1” 评分每次增加的值(最小变化值)
(4)android:isIndicator=“false” 默认为false,如果为true,RatingBar的进度不能被人为修改
(5)样式:
默认样式:style="?attr/ratingBarStyle" 或 style="@style/Widget.AppCompat.RatingBar"
指示器样式:style="?attr/ratingBarStyleIndicator" 或 style="@style/Widget.AppCompat.RatingBar.Indicator"
小型样式:style="?attr/ratingBarStyleSmall" 或 style="@style/Widget.AppCompat.RatingBar.Small"
(6)自定义样式:android:progressDrawable="@drawable/rating_bg"

7.Switch

<Switch
        android:id="@+id/sw"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:switchMinWidth="100dp"
        android:showText="true"
        android:textOn="ON"
        android:textOff="OFF"/>

知识点:
(1)android:checked=“true” 初始状态(默认为false,即"关"),这里设置为"开"
(2)android:switchMinWidth=“100dp” 设置开关的最小宽度
(3)android:showText=“true” 设置on/off的时候是否显示文字
android:textOn=“ON” 按钮"开"状态显示的文字
android:textOff=“OFF” 按钮"关"状态显示的文字

8.ToggleButton

<ToggleButton
        android:id="@+id/tb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOn="开"
        android:textOff="关"/>

9.ScrollView
注意:ScrollView的直接子View只能有一个。也就是说如果要使用很复杂的视图结构,就必须把这些视图放在一个标准布局里,如LinearLayout、RelativeLayout等。如果只有一个视图,如控件TextView,直接放在ScrollView中也是可以的。
一些常用属性:
(1)android:scrollbars=“none” 滚动条隐藏
(2)android:overScrollMode=“never” 到顶下拉/到底上拉出现的波纹阴影隐藏
在这里插入图片描述

10.AlertDialog
AlertDialog即弹出框,用于提示用户和进行简单交互,可以提供最多三个按钮(确认setPositiveButton,取消setNegativeButton,中立setNeutralButton)、标题(title)和提示信息(message)。可以通过方法直接设置,如果需要呈现复杂布局,也可以自定义布局并设置。

11.单选控件RadioGroup与RadioButton
在这里插入图片描述

12.复选框CheckBox
在这里插入图片描述

13.触屏事件
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值