Android常用控件(更新中)

本文整理了Android中常见的控件,包括TextView、EditText、ImageView、Button、ImageButton、ProgressBar、AlertDialog、ProgressDialog、AutoCompleteTextView、MultiAutoCompleteTextView、ToggleButton、CheckBox、RadioButton、RadioGroup、Intent、ListView、DatePicker、TimePicker、DatePickerDialog、TimePickerDialog和GridView,详细介绍了各控件的作用、属性及使用示例,便于理解和应用。
摘要由CSDN通过智能技术生成

最近在学Android,前后算起来都半个月了,可还是没什么进步,都是些控件。发现控件虽不难,但是很多,脑子一团凌乱,因此整理了一下,以便使用。

1. TextView

作用:显示文本框。
常用属性:

android:id  --控件的id
android:layout_width  --控件宽度
android:layout_height  --控件高度
android:text  --文本内容
android:textSize  --字体大小
android:textColor  --字体颜色
android:background  --控件背景(格式:#123def,或用图片)
android:singleLine="true" --让这个 TextView 只能单行显示
android:padding="10dp" --给控件的周围加上补白,不让文本紧靠边缘
android:ellipsize="end" --设定文本内容超出控件宽度时,文本的缩略方式(这里指定成 end 表示在尾部进行缩略)

注意:若在XML中引用一个id,则使用@id/id_name语法;而若在XML中定义一个id,则要使用@+id/id_name语法。

2. EditText

作用:编辑文本框。
常用属性:

android:id  --控件的id
android:layout_width  --控件宽度
android:layout_height  --控件高度
android:text  --文本内容
android:textSize  --字体大小
android:textColor  --字体颜色
android:background  --控件背景
——————前面同TextView——————————
android:hint --提示文本
android:inputType --输入文本类型
android:maxLines --最大行数

TextView和EditText如图所示:
这里写图片描述

3. ImageView

作用:显示图片
常用属性:

android:src="@drawable/ic_launcher"  --ImageView内容图片(引号内为文件路径)
android:background="@drawable/ic_launcher"  --ImageView背景图片
android:background="#ff0ff0"  --ImageView背景颜色
android:scaleType="fitXY" --让这张图片填充满整个控件

注:除在XML文件中指定图片资源外,还可在代码区更改图片。
示例代码:

imageView.setImageResource(R.drawable.zoro);

4. Button

作用:按钮
常用属性:

<Button
        android:id= "@+id/button1" --定义id用@+id/id_name语法;引用id则用@id/id_name语法
        android:layout_width= "match_parent"
        android:layout_height= "wrap_content"
        android:text= "@string/button_name" --注意:文本常用该方式调用
        />

5. ImageButton

作用:图片按钮
常用属性:

< ImageButton
        android:id= "@+id/imageButton1"
        android:layout_width= "wrap_content"
        android:layout_height= "wrap_content"
        android:src= "@drawable/ic_launcher"
        android:background= "#000000" />

注:
1. ImageButton的android:text属性不能直接赋值。
2. Button

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值