Android开发---控件

120 阅读2分钟

开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第11天,点击查看活动详情

控件

常用控件为:

ScrollView 滚动条容器

TextView 显示文字,相当于Panel

ImageView 显示图片

EditText 输入框,可编辑,可设置软键盘方式

Button 按钮,可附带图片

CheckBox 复选框

RadioButton 单选按钮(和 RadioGroup 配合使用)

控件属性

控件属性分为布局属性和外观属性

1.布局属性

1.1 线性布局时

android:layout_gravity="center" 水平居中

android:layout_margin="10dp" 距离周围其他控件10dp

android:layout_weight="1" 所占尺寸权重

1.2 相对布局时android:layout_below="@+id/et_number" 在某元素的下方

android:layout_alignParentBottom="true" 放置于父窗体下沿

android:layout_marginTop="100dp" 距离父容器上部100dp

2.外观属性

2.1 TextView

知识点:单位dp和sp,文字大小统一用sp为单位,其他尺寸一律为dp

android:text="显示文本" 显示文本

android:textSize="30sp" 文字大小

android:textColor="#FF0000" 文本颜色

android:background="#0000FF" 背景颜色

android:padding="20dp" 内容距离边界20dp

android:drawableBottom 在文字下面绘制图片

android:gravity="center" 文字居中显示

android:sigleline 则控件的内容在同一行中进行显示(和ellipsize一起使用)

android:ellipsize 设置当文字过长时,省略号该如何显示start,end,middle,marquee

android:lineHeight 行高,多行显示时控制行间距,多行设置

android:lineSpacingExtra行间距,指定数值,例如10sp,多行设置

android:lineSpacingMultiplier 多倍行距,多行设置

2.2 ImageView

android:src="@mipmap/ic_launcher" 图片路径

android:scaleType="fifitXY" 图片填充模式

知识点:android应用中所有的图片资源都是png格式,放在mipmap文件夹下,可以用

@mipmap/pic_name来定位图片资源,pic_name.png为图片名字

2.3 EditText

android:hint 设置edittext为空时输入框内的提示信息

android:text 指定控件当中显示的文字

android:textstyle设置字形[bold(粗体) 0, italic(斜体) 1, bolditalic(又粗又斜) 2]

android:textsize 指定控件当中字体的大小

android:background 指定该控件所使用的背景色,rgb命名法

2.4 ScrollView

通常为根标签,为整个窗体加入滚动条

三、线性布局

源文件:app1_UI/MainActivity.java、layout/linear_layout.xml

<LinearLayout xmlns:android="schemas.android.com/apk/res/and…"

xmlns:app="schemas.android.com/apk/res-aut…"

xmlns:tools="schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="horizontal"

tools:context=".MainActivity">

<TextView四、相对布局

源文件:app1_UI/MainActivity.java、layout/relative_layout.xml

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:gravity="center"

android:layout_weight="1"

android:drawableTop="@mipmap/topay"

android:text="待付款"/>

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:gravity="center"

android:layout_weight="1"

android:drawableTop="@mipmap/touse"

android:text="待使用"/>

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:gravity="center"

android:layout_weight="1"

android:drawableTop="@mipmap/tocomment"

android:text="待评价"/>

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:gravity="center"

android:layout_weight="1"

android:drawableTop="@mipmap/refund"

android:text="退款/售后"/>

<RelativeLayout xmlns:android="schemas.android.com/apk/res/and…"

android:layout_width="match_parent" android:layout_height="match_parent">

<ImageView

android:background="@mipmap/bg"

android:layout_width="match_parent"

android:layout_height="wrap_content" />

<ImageView

android:id="@+id/head"

android:background="@mipmap/head"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_margin="5dp"/>

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="edQ971111111"

android:textColor="#000000"五、容器嵌套

源文件:app1_UI/MainActivity.java、layout/contain_layout.xml

android:textStyle="bold"

android:layout_marginTop="10dp"

android:layout_marginLeft="20dp"

android:layout_toRightOf="@+id/head"

android:textSize="10sp"/>

<ImageView

android:background="@mipmap/back"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:rotation="180"

android:layout_alignParentRight="true"

android:layout_marginTop="15dp"/>

<LinearLayout xmlns:android="schemas.android.com/apk/res/and…"

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent">

<EditText

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="请输入姓名"/>

<EditText

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="请输入密码"/>

<RadioGroup

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal">

<RadioButton

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="男"/>

<RadioButton

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="女"/>

<LinearLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"android:orientation="horizontal">

<CheckBox

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="游泳"/>

<CheckBox

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="足球"/>

<CheckBox

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="篮球"/>

<CheckBox

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="旅游"/>

<CheckBox

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="摄影"/>

<Button

android:text="确定"

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第11天,点击查看活动详情