zl程序教程

您现在的位置是:首页 >  APP

当前栏目

【Android从零单排系列五】《Android视图控件——控件基本属性说明》

2023-03-07 09:08:15 时间

目录

前言

一.属性介绍

二.Demo示例

前言

小伙伴们,在前面的几篇文章中,我们介绍了android发展的历史、android开发的基本介绍、开发工具、环境搭建、并且成功的跑起来了我们的第一个程序。

那么从本文开始就进入了一个新的阶段,我们要开始学习写android程序的界面了。但是在写界面之前,我们这篇文章先重点介绍一下android视图控件的基本属性。

一.属性介绍

1.android:id   组件id 2.android:layout_width  宽度 3.android:lauout_height  高度         设置宽高默认有几种常用的属性 wrap_content(自适应)、match_parent(充满父布局) 4.android:text  控件显示文本内容 5.android:textColor 设置文本颜色         这里一般把颜色写到  res->values->colors.xml文件中 6.android:textSize 设置文本字体大小  Android 中的长度单位详解 7.android:ellipsize              当文字长度超过textview宽度时的省略显示方式         "start"省略号显示在开头         "end"省略号显示在结尾         "middle"省略号显示在中间         "marquee"以横向滚动方向显示(需要获取当前焦点) 8. android:layout_margin="10dp"距离上下左右侧的距离 9.android:layout_marginLeft="10dp"距离左侧的距离 10.android:layout_marginRight="10dp"距离右侧的距离 11.android:layout_marginBottom="10dp"距离下侧的距离 12.android:layout_marginTop="10dp"距离上侧的距离 13.android:layout_centerHrizontal 水平居中 (Hrizontal表示水平) 14.android:layout_centerVertical 垂直居中 (Vertiacl表示垂直) 15.android:layout_centerInparent 相对于父元素完全居中 16.android:layout_alignParentBottom 贴紧父元素的下边缘 (align 表示使什么成为一行) 17.android:layout_alignParentLeft 贴紧父元素的左边缘 18.android:layout_alignParentRight 贴紧父元素的右边缘 19.android:textStyle 设置文本字体样式 20.android:typeface 设置文本字体         字体类型         Typeface.DEFAULT:默认字体,常规字体类型         Typeface.DEFAULT_BOLD:黑体字体类型         Typeface.MONOSPACE:等宽字体类型         Typeface.SANS_SERIF:sans serif字体类型         字体样式         Typeface.BOLD //粗体         Typeface.BOLD_ITALIC //粗斜体         Typeface.ITALIC //斜体         Typeface.NORMAL //常规 21.android:visibility

设置控件可见不可见visible(可见)、 invisible(占位置不可见)、 gone(不可见) 22.android:layout_weight

设置控件的权重 23.android:onClick  

设置点击事件  给控件添加点击事件 24.android:padding 控件里的内容距离控件边的距离

25.android:background 设置背景颜色 26. android:alpha设置透明度

二.Demo示例

   <TextView
        android:id="@+id/name_tv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="【Android从零单排系列五】
        《Android视图控件——控件基本属性说明》再见孙悟空"
        android:textColor="@color/black"
        android:background="@android:color/holo_blue_bright"
        android:textSize="12sp"
        android:layout_margin="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginBottom="100dp"
        android:layout_marginTop="1000dp"
        android:layout_marginHorizontal="100dp"
        android:layout_marginVertical="100dp"
        android:visibility="visible"
        android:typeface="sans"
        android:alpha="@integer/material_motion_duration_long_1"
        />

看下运行后的效果,比较丑...凑合看吧...懒得改了...