Android常用控件

Android常用控件

  • 文本类控件
    TextView 负责展示文本,非编辑
    EditText 可编辑文本控件
  • 按钮类控件
    Button 按钮
    ImageButton 图片按钮
    RadioButton与RadioGroup 单选按钮
    CheckBox 复选按钮
  • 图片控件
    ImageView 负责显示图片
  • 进度条控件
    ProgressDialog 等待进度条
    ProgressBar 条形进度条

1.TextView 文本控件

  • 控件属性

(1).android:id:控件id,控件唯一标识符。

(2).android:layout_width:控件的宽度。

(3).android:layout_height:控件的高度。

(4).android:gravity:指定TextView中文字对齐方式,有多种方式,我们在此选的是center,居中显示。

(5).android:textSize:指定TextView中文字文字的大小。

(6).android:textColor:指定TextView中文字的颜色,是16进制的色值。

(7).android:text:指定TextView显示内容。


    <TextView
        android:layout_width="match_parent" //wrap_content或者match_parent
        android:layout_height="match_parent" //wrap_content或者match_parent
        android:gravity="center" //来指定文字的对齐方式,可选值有 top、bottom、left、right、center 等
        android:id="@+id/text_view"
        android:textSize="30sp" //以sp为单位
        android:textColor="#FF6A6A" //RGB颜色
        android:text=" Activity控件"
        />
  • 效果如下
    这里写图片描述

2.EditText 可编辑文本控件

  • 控件属性
    其他控件与textview相同
    (1).android:password:输入内容设置为password类型。
    (2).android:phoneNumber:输入内容设置为phoneNumber类型。
    (3).android:hint:提示用户该输入框是干嘛的。
<EditText
        android:id="@+id/edit_text"
        android:layout_width="match_parent" //wrap_content或者match_parent
        android:layout_height="wrap_content" //wrap_content或者match_parent
        android:password="true" //输入内容设置为password类型,输入的内容会变成****
        android:hint="输入密码"//提示文字

        />
    <EditText
        android:id="@+id/edit_text_2"
        android:layout_width="match_parent"//wrap_content或者match_parent
        android:layout_height="wrap_content"//wrap_content或者match_parent
        android:phoneNumber="true"//输入内容设置为phoneNumber类型,只能输入数字
        android:hint="输入数字"//提示文字

        />
  • 效果如下
    这里写图片描述 这里写图片描述

3.Button按钮

 <Button
        //控件id
        android:id="@+id/button"
        //宽度与高度
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        //按钮上显示的文字
        android:text="Button按键"
        //按钮字体大小
        android:textSize="20sp"

        />

使用findViewById来通过Id获取该按钮,获取按钮后我们需要给按钮绑定点击事件。也就是点击按钮要做的事情。
(1).接口回调的形式绑定点击事件

        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 //点击按钮要做的事情
            }
         });

(2)委托代理

     button.setOnClickListener(this);

      //重写委托回调的方法
     /**
      * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
   @Override
    public void onClick(View v) {
        switch (v.getId()){
             case R.id.button:
                //点击按钮后要做的事情
                break;
            default:
                break;
        }
     }    
  • 效果如下
    这里写图片描述

4.ImageButton 图片按钮

<ImageButton
        android:id="@+id/button"
        //具体设置高度和宽度显示的像素,不过这样设置如果图片尺寸大于设置的显示的尺寸,则图片是显示不全的,这是可以配合android:scaleType属性。
        android:layout_width="250sp"
        android:layout_height="200sp"
        //把原图按照指定的大小在View中显示,拉伸显示图片,不保持原比例,填满ImageButton.
        android:scaleType="fitXY"
        //其他的关于android:scaleType的参数
        //android:scaleType="center"  在视图中心显示图片,并且不缩放图片
        //android:scaleType="centercrop"  按比例缩放图片,使得图片长 (宽)的大于等于视图的相应维度
        //android:scaleType="centerinside"  按比例缩放图片,使得图片长 (宽)的小于等于视图的相应维 度
        //android:scaleType="fitcenter" 按比例缩放图片到视图的最小边,居中显示
        //android:scaleType="fitend" 按比例缩放图片到视图的最小边,显示在视图的下部分位置
        //android:scaleType="fitstart" 把图片按比例扩大/缩小到视图的最小边,显示在视图的上部分位置
        //android:scaleType="matrix" 用矩阵来绘制

        //图片来源,需要将图片复制放到res/drawable文件夹里面,引用的时候不需要写图片的后缀
        android:src ="@drawable/tu1"
        />
  • 效果如下
    这里写图片描述

5.RadioButton与RadioGroup 单选按钮

  • RadioButton 单选按钮
  • RadioGroup 单选组合框
<RadioGroup
        android:id="@+id/radio_group"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        //设置RadioButton的排列方式,分为水平排列horizontal与垂直排列vertical
        android:orientation="horizontal" >
    <RadioButton
        android:id="@+id/rdio1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"
         //设置单选后的提示文字
        android:text="北京"/>
    <RadioButton
        android:id="@+id/rdio2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"
         //设置单选后的提示文字
        android:text="上海" />
    </RadioGroup>
  • 效果如下
    这里写图片描述

6.CheckBox 复选按钮

<CheckBox
        android:id="@+id/checkb1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        //设置复选按钮后的提示文字
        android:text="北京"
        android:textSize="30sp"/>
    <CheckBox
        android:id="@+id/checkb2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        //设置复选按钮后的提示文字
        android:text="上海"
        android:textSize="30sp"
        />
  • 效果如下
    这里写图片描述

7.ImageView 负责显示图片

<ImageView
    android:id = "@+id/image"
    //具体设置高度和宽度显示的像素,不过这样设置如果图片尺寸大于设置的显示的尺寸,则图片是显示不全的,这是可以配合android:scaleType属性。
    android:layout_width="375dp"
    android:layout_height="300dp"
    //把原图按照指定的大小在View中显示,拉伸显示图片,不保持原比例,填满ImageButton.
    android:scaleType="fitXY"
    //其他的关于android:scaleType的参数
    //android:scaleType="center"  在视图中心显示图片,并且不缩放图片
    //android:scaleType="centercrop"  按比例缩放图片,使得图片长 (宽)的大于等于视图的相应维度
    //android:scaleType="centerinside"  按比例缩放图片,使得图片长 (宽)的小于等于视图的相应维 度
    //android:scaleType="fitcenter" 按比例缩放图片到视图的最小边,居中显示
    //android:scaleType="fitend" 按比例缩放图片到视图的最小边,显示在视图的下部分位置
    //android:scaleType="fitstart" 把图片按比例扩大/缩小到视图的最小边,显示在视图的上部分位置
    //android:scaleType="matrix" 用矩阵来绘制

    //图片来源,需要将图片复制放到res/drawable文件夹里面,引用的时候不需要写图片的后缀
    android:src ="@drawable/tu1"/>
  • 效果如下
    这里写图片描述

8.ProgressBar进度条

<ProgressBar 
    android:id="@+id/pb"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    //默认是圆形进度条,可以知道样式设置为水平进度条
    style="?android:attr/progressBarStyleHorizontal"/>
    //指定成水平进度条后,我们还可以通过 android:max属性给进度条设置一个最大值,然后在代码中动态地更改进度条的进度
    android:max="100"/>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值