Android基础控件的使用

目录

TextView

文本设置边框

EditText(输入框)

EditText 特有属性

ImageView(图像视图)

ImageView的基本使用

Button(按钮)

Button的基本使用


TextView

文本常用属性:

属性名作用
id为TextView设置一个组件id,根据id,我们可以在Java代码中通过findViewById()的方法获取到该对象,然后进行相关属性的设置
layout_width组件的宽度
layout_height组件的高度
gravity设置控件中内容的对齐方向,TextView中是文字,ImageView中是图片等等
text设置显示的文本内容,一般我们是把字符串写到string.xml文件中,然后通过@String/xxx取得对应的字符串内容的
textColor设置字体颜色,同上,通过colors.xml资源来引用
textStyle设置字体风格,三个可选值:normal(无效果),bold(加粗),italic(斜体)
textSize字体大小,单位一般是用sp
background控件的背景颜色,可以理解为填充整个控件的颜色,可以是图片
autoLink识别链接类型 (web, email, phone ,map ,none, all)

示例:

<TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
         />

其中layout_width与layout_height是必须有的属性

文本设置边框

  • 实现原理:

    编写一个ShapeDrawable的资源文件!然后TextView将 background 设置为这个drawable资源即可

    • ShapeDrawable的资源文件

    • <solid android:color="xxx" > 这个是设置背景颜色的

    • <stroke android:width="xdp" android:color="xxx"> 这个是设置边框的粗细,以及边框颜色的

    • <padding androidLbottom="xdp"> 这个是设置边距的

    • <corners android:topLeftRadius="10px"> 这个是设置圆角的

    • <gradient> 这个是设置渐变色的,可选属性有: startColor:起始颜色 endColor:结束颜色 centerColor:中间颜色 angle:方向角度,等于0时,从左到右,然后逆时针方向转,当angle = 90度时从下往上 type:设置渐变的类型

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 设置一个黑色边框 -->
    <stroke android:width="2px" android:color="#000000"/>
    <!-- 渐变 -->
    <gradient
    android:angle="270"
    android:endColor="#C0C0C0"
    android:startColor="#FCD209" />
    <!-- 设置边距 -->
    <padding
    android:left="5dp"
    android:top="5dp"
    android:right="5dp"
    android:bottom="5dp"/>
</shape>

圆角示例:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 设置透明背景色 -->
    <solid android:color="#87CEEB" />
    <!-- 设置一个黑色边框 -->
    <stroke
    android:width="2px"
    android:color="#000000" />
    <!-- 设置四个圆角的半径 -->
    <corners
    android:bottomLeftRadius="10px"
    android:bottomRightRadius="10px"
    android:topLeftRadius="10px"
    android:topRightRadius="10px" />
    <!-- 设置边距 -->
    <padding
    android:bottom="5dp"
    android:left="5dp"
    android:right="5dp"
    android:top="5dp" />
</shape>

EditText(输入框)

      

EditText 特有属性

属姓名说明
android:hint默认提示文本
android:textColorHint默认提示文本的颜色
android:selectAllOnFocus布尔值。点击输入框获得焦点后,获取到输入框中所有的文本内容
android:inputType对输入的数据进行限制
android:minLines设置最小行数
android:maxLines设置最大行数 PS:当输入内容超过maxline,文字会自动向上滚动!!
android:singleLine只允许单行输入,而且不会滚动
android:textScaleX设置字与字的水平间隔
android:textScaleY设置字与字的垂直间隔
android:capitalizesentences:仅第一个字母大写 ;words:每一个单词首字母大小,用空格区分单词;characters:每一个英文字母都大写

示例:

        

<EditText
        android:id="@+id/editTextTextPersonName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Name" />

文本框有那些类型

        

android:inputType="none" 
android:inputType="text" 
android:inputType="textCapCharacters" 
android:inputType="textCapWords" 
android:inputType="textCapSentences" 
android:inputType="textAutoCorrect" 
android:inputType="textAutoComplete" 
android:inputType="textMultiLine" 
android:inputType="textImeMultiLine" 
android:inputType="textNoSuggestions" 
android:inputType="textUri" 
android:inputType="textEmailAddress" 
android:inputType="textEmailSubject" 
android:inputType="textShortMessage" 
android:inputType="textLongMessage" 
android:inputType="textPersonName" 
android:inputType="textPostalAddress" 
android:inputType="textPassword" 
android:inputType="textVisiblePassword" 
android:inputType="textWebEditText" 
android:inputType="textFilter" 
android:inputType="textPhonetic"

数值类型

android:inputType="number" 
android:inputType="numberSigned" 
android:inputType="numberDecimal" 
android:inputType="phone"//拨号 
android:inputType="datetime" 
android:inputType="date"//日期 
android:inputType="time"//时间

ImageView(图像视图)

        

ImageView的基本使用

属性名说明
android:src设置图片资源
android:scaleType设置图片缩放类型
android:maxHeight最大高度
android:maxWidth最大宽度
android:adjustViewBounds调整View的界限

src与background的区别

在API文档中我们发现ImageView有两个可以设置图片的属性,分别是:

src和background 常识:

① background通常指的都是背景,而src指的是内容!!

② 当使用src填入图片时,是按照图片大小直接填充,并不会进行拉伸,而使用background填入图 片,则是会根据ImageView给定的宽度来进行拉伸

但是src有一个效果一样的兄弟叫srcCompat

示例:

<ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        app:srcCompat="@drawable/shape" />

Button(按钮)

Button的基本使用

属性名描述
android:autoText如果设置,指定该TextView中有一个文本输入法,并自动纠正一些常见的拼写错误
android:drawableBottom可拉伸要绘制的文本的下面
android:drawableRight可拉伸要绘制的文本的右侧
android:editable如果设置,指定该TextView有一个输入法
android:text要显示的文本
android:background这是一个可拉伸使用来做背景
android:id对此视图提供一个标识符名称
android:visibility控制视图的初始可视性
android:onClick在本视图的上下文视图被点击时调用的方法的名称
android:contentDescription定义文本简要介绍了视图内容

示例:

<Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button" />

点击事件示例:

button2.setOnClickListener(new OnClickListener() {  //按钮绑定点击事件
      @Override
      public void onClick(View v) {
        btnTwo.setText("按钮可用");
     }
   });

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值