Android中的UI基本控件

    xiaoxiao2021-03-25  97

    什么是UI控件?

    我们小的时候都玩过积木,我们将样子相同不同的积木根据不同的摆放位置拼凑成不同的东西,而我们的UI控件就像是那一个个积木,只是这些积木是在我们的Android手机上展示的,我们称它为UI控件,每一个UI控件都有它的作用

    UI控件与View及其子类的关系?

    Android应用的绝大部分UI组件都放在android.widget包及其子包中,android.view包及其子包中,Android应用的所有的UI组件都继承了View类,View代表了一块空白的矩形区域。

    2.1 View

    View与ViewGroup的概念与关系

    继承了View的7UI类

    基本控件

    TextView EditText RadioButton Button CheckBox ImageView ImageButton

    ViewGroup

    容器类控件

    LinearLayout RelativeLayout FrameLayout ListView Spinner GridView

    常用的属性

    android:id=”@+id/viewId” android:layout_width=”match_parent|wrap_content” android:layout_height=”match_parent|wrap_content” android:background=”#abc” android:padding=”“内边矩-内容距离控件 android:layout_margin=”“外边距-控件之间的距离

    与属性对应的方法

    setMaxLines(int) setLines(int) setSingleLine(boolean) setTextColor(int) setTextSize(float) setText(String)

    setAutoLinkMask(int)

    Linkify.WEB_URLS Linkify.EMAIL_ADDRESS Linkify.PHONE_NUMBERS Linkify.MAP_ADDRESS Linkify.ALL

    UI布局的概念:将控件按照一定的规则放置

    常用的布局有

    3.1 LinearLayout 线性布局

    orientation=”horizontal | vertical” 将其直接子控件按照竖直或水平的方向一一排放

    layout_weight horizontal->width=”0dp” vertical ->height=”0dp” 权重分配的是剩余的空间 剩余空间>0 权重越大,所占空间越大 剩余空间<0 权重越大,所占空间越小

    android:gravity 控件内容及子控件的对齐方式(View本身)

    top left right bottom center center_horizontal center_vertical

    android:layout_gravity 控件在父布局中的对齐方式

    3.2 RelativeLayout 相对布局 :将其子控件对应其兄弟控件的位置或父容器的相对位置排放

    3.2.1相对的兄弟控件

    放置的位置

    layout_toLeftOf 在指定的兄弟控件的左边位置 layout_toRightOf 在指定的兄弟控件的右边位置 layout_below 在指定的兄弟控件下边位置 layout_above 在指定的兄弟控件上边位置

    对齐方式

    layout_alignLeft 与指定的兄弟控件左对齐 layout_alignRight 与指定的兄弟控件右对齐 layout_alignTop 与指定的兄弟控件顶部对齐 layout_alignBottom 与指定的兄弟控件底部对齐 layout_alignBaseline 与指定的兄弟控件内容对齐

    3.2.2 相对的父控件

    位置对齐

    layout_alignParentLeft 放置在父控件的左边(默认方式) layout_alignParentRight 放置在父控件的右边 layout_alignParentTop 放置在父控件顶部 layout_alignParentBottom 放置在父控件的底部

    居中对齐

    layout_centerInParent 放置在父控件的居中位置 layout_centerHorizontal 放置在父控件水平居中位置 layout_centerVertical 放置在父控件垂直居中位置

    3.3 FrameLayout 帧布局:将子控件一层一层绘画在父容器内

    android:margin 设置组件的距离 android:layout_gravity 设置子控件位置

    3.4 TableLayout 表格布局:唯一一个可以不定义宽高的布局

    TableRow-行 stretchColumns=”1”拉伸列 layout_column=”1”将某列强制转成第1列 layout_span=”2”当前列占用2列

    3.5 Absolutelayout 绝对布局

    layout_x=”256dp” layout_y=”100dp” 在手机上是不会用这个布局的 在做智能电视的时候可能会有一下

    注*

    因为TextView在Android中比较重要在这里就重点写一下这其中的一些常用的控件父类是View 文本内容显示控件

    常用的属性有

    text 显示内容的属性 textSize 显示内容的字体大小,单位是sp textColor 显示内容的字体颜色,格式“#RGB” singleLine 是否单行显示 lines 初始显示的行数 maxLines 最大显示的行数 padding 内边框间距 layout_margin 外边框间距

    autoLink 自动链接

    ellipsize 用省略号显示超出控件宽度的文本内容

    autoLink又有一些功能

    none 无链接 web 网址 email 电子邮箱 phone 电话号码 map 地理位置 all 包含以上类型

    ellipsize 也有一些功能 start 开如位置省略 middle 中间位置省略 end 末尾位置省略 marquee 滚动显示

    Android中TextView也可以用跑马灯效果

    android:singleLine=”true”设置单行 android:ellipsize=”marquee”设置超出文本为滚动 android:marqueeRepeatLimit=”marquee_forever” 滚动重复次数限制 android:focusable=”true” 获取焦点 android:focusableInTouchMode=”true” 触摸方式的获取焦点

    转载请注明原文地址: https://ju.6miu.com/read-18549.html

    最新回复(0)