常用控件

TextView

它主要用于在界面上显示一段文本信息

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:id="@+id/textView"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:text="This is TextView"/>
</LinearLayout>
  • android:id 给当前控件定义了一个唯一标识符
  • android:layout_width/android:layout_height: Android中所有的控件都具有这两个属性,可选值有3种:match_parent、wrap_content和固定值。match_parent表示让当前控件的大小和父布局的大小一样,也就是由父布局来决定当前控件的大小。wrap_content表示让当前控件的大小能够刚好包含住里面的内容,也就是由控件内容决定当前控件的大小。固定值表示表示给控件指定一个固定的尺寸,单位一般用dp,这是一种屏幕密度无关的尺寸单位,可以保证在不同分辨率的手机上显示效果尽可能地一致,如50 dp就是一个有效的固定值。
  • android:text 要显示的文字
  • android:textSize 文字的大小,一般以sp作为单位
  • android:textColor 文字的颜色,“#RGB的16进制值,如”#ff0000"或者"@color/…"
  • android:gravity TextView的文字对齐方式,可选值有top、bottom、start、end、center等,可以用“|”来同时指定多个值,这里我们指定的是"center",效果等同于"center_vertical|center_horizontal",表示文字在垂直和水平方向都居中对齐

Button

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent">...<Buttonandroid:id="@+id/button"android:layout_width="match_parent"android:layout_height="wrap_content"android:textAllCaps="false"android:text="Button" />
</LinearLayout>

Button是程序用于和用户进行交互的一个重要控件

  • android:textAllCaps 所有字母是否全部大小写 true、false

EditText

EditText是程序用于和用户进行交互的另一个重要控件,它允许用户在控件里输入和编辑内容,并可以在程序中对这些内容进行处理。

  • android:hint=“Type something here” 指定了一段提示性的文本,一旦用户输入了任何内容,这些提示性的文字就会消失。
  • android:maxLines 指定了EditText的最大行数,这样当输入的内容超过该数时,文本就会向上滚动,EditText则不会再继续拉伸。

通过editText对象的getText().toString() 可以获取到editText框里的字符并以字符串的形式返回。

ImageView

ImageView是用于在界面上展示图片的一个控件。图片通常是放在以drawable开头的目录下的,并且要带上具体的分辨率。现在最主流的手机屏幕分辨率大多是xxhdpi的,所以我们在res目录下再新建一个drawable-xxhdpi目录,然后将事先准备好的图片img_1.png和img_2.png复制到该目录当中。

  • android:src=“@drawable/img_1” 性给ImageView指定了一张图片,由于图片的宽和高都是未知的,所以将ImageView的宽和高都设定为wrap_content,这样就保证了不管图片的尺寸是多少,都可以完整地展示出来。

还可以在程序中通过代码动态地更改ImageView中的图片。通过ImageView对象的setImageResource() 方法改变成成指定的图片。eg:imageView.setImageResource(R.drawable.img_2)

ProgressBar

ProgressBar用于在界面上显示一个进度条,表示我们的程序正在加载一些数据。会看到屏幕中有一个圆形进度条正在旋转。

  • android:visibility Android控件的可见属性,所有的Android控件都具有这个属性。可选值有3种:visible、invisible和gone。visible表示控件是可见的,这个值是默认值,不指定android:visibility时,控件都是可见的。invisible表示控件不可见,但是它仍然占据着原来的位置和大小,可以理解成控件变成透明状态了。gone则表示控件不仅不可见,而且不再占用任何屏幕空间。可以通过代码来设置控件的可见性,通过ProgressBar对象的getVisibility()方法来判断是否可见,使用是setVisibility()方法,允许传入View.VISIBLE、View.INVISIBLEView.GONE这3种值来设置可见性。
  • style=“?android:attr/progressBarStyleHorizontal” 通过style属性可以将他指定成水平进度条。
  • android:max 指定成水平进度条后,我们还可以通过android:max属性给进度条设置一个最大值,然后在代码中动态地更改进度条的进度。

AlertDialog

AlertDialog可以在当前界面弹出一个对话框,这个对话框是置顶于所有界面元素之上的,能够屏蔽其他控件的交互能力,因此AlertDialog一般用于提示一些非常重要的内容或者警告信息。比如为了防止用户误删重要内容,在删除前弹出一个确认对话框。

首先通过AlertDialog.Builder 构建一个对话框,为这个对话框设置标题、内容、可否使用Back键关闭对话框等属性。接下来调用setPositiveButton() 方法为对话框设置确定按钮的点击事件,调用setNegativeButton() 方法设置取消按钮的点击事件,最后调用show() 方法将对话框显示出来就可以了。


效果如图:

几种基本布局

LinearLayout

  • android:orientation vertical/horizontal, 是LinearLayout标签的属性,用于指定布局中线性排列的方向。
  • android:layout_gravity 用于指定控件在布局中的对齐方式,android:layout_gravity的可选值和android:gravity差不多,但是需要注意,当LinearLayout的排列方向是horizontal时,只有垂直方向上的对齐方式才会生效;当LinearLayout的排列方向是vertical时,只有
    水平方向上的对齐方式才会生效。
    layout文件activity_main.xml如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="horizontal"android:layout_width="match_parent"android:layout_height="match_parent"><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/button_1"android:text="Button_1"android:textAllCaps="false"android:layout_gravity="top"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/button_2"android:text="Button_2"android:textAllCaps="false"android:layout_gravity="bottom"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/button_3"android:text="Button_3"android:textAllCaps="false"android:layout_gravity="center_vertical"/></LinearLayout>

activity_main.xml的显示效果如图:

  • android:layout_weight 这个属性允许我们使用比例的方式来指定控件的大小,在手机屏幕的适配性方面可以起到非常重要的作用

RelativeLayout

RelativeLayout又称作相对布局,也是一种非常常用的布局。和LinearLayout的排列规则不同,RelativeLayout显得更加随意,它可以通过相对定位的方式让控件出现在布局的任何位置。

  1. 以下控件属性都是相对于父布局进行定位的:
  • android:layout_alignParentLeft 与父控件元素左对齐
  • android:layout_alignParentTop 与父控件元素上对齐
  • android:layout_alignParentRight 与父控件元素右对齐
  • android:layout_alignParentBottom 与父控件元素底对齐
  • android:layout_centerInParent 在父控件元素居中位置

用法如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/button_1"android:text="Button_1"android:layout_alignParentLeft="true"android:layout_alignParentTop="true"android:textAllCaps="true" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/button_2"android:text="Button_2"android:layout_alignParentRight="true"android:layout_alignParentTop="true"android:textAllCaps="true" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/button_3"android:text="Button_3"android:layout_alignParentLeft="true"android:layout_alignParentBottom="true"android:textAllCaps="true" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/button_4"android:text="Button_4"android:layout_alignParentRight="true"android:layout_alignParentBottom="true"android:textAllCaps="true" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/button_5"android:text="Button_5"android:layout_centerInParent="true"android:textAllCaps="true" />
</RelativeLayout>

效果如图:

  1. 以下控件属性都是相对于具体的控件进行定位的,,需要为这个属性指定相对控件id的引用:
  • android:layout_above 让一个控件位于另一个控件的上方
  • android:layout_below 让一个控件位于另一个控件的下方
  • android:layout_toLeftOf 让一个控件位于另一个控件的左侧
  • android:layout_toRightOf 让一个控件位于另一个控件的右侧

用法如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/button_5"android:text="Button_5"android:layout_centerInParent="true"android:textAllCaps="true" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/button_6"android:text="Button_6"android:layout_above="@id/button_5"android:layout_toLeftOf="@id/button_5" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/button_7"android:text="Button_7"android:layout_above="@id/button_5"android:layout_toRightOf="@id/button_5" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/button_8"android:text="Button_8"android:layout_below="@id/button_5"android:layout_toLeftOf="@id/button_5" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/button_9"android:text="Button_9"android:layout_below="@id/button_5"android:layout_toRightOf="@id/button_5" />
</RelativeLayout>

效果如图:

FrameLayout

FrameLayout又称作帧布局,它相比于前面两种布局就简单太多了,这种布局没有丰富的定位方式,因此它的应用场景少了很多。所有的控件都会默认摆放在布局的左上角

  1. 布局文件如下
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><EditTextandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:hint="Enter something"/><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@mipmap/ic_launcher"/>
</FrameLayout>

效果如图:
ImageView在EditText后面,所以把EditText覆盖了。

  1. 除上面的默认布局之外…也可以使用layout_gravity指定控件对齐方式。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><EditTextandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:hint="Enter something"android:layout_gravity="left"/><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@mipmap/ic_launcher"android:layout_gravity="right"/>
</FrameLayout>

效果如图所示:

Android-UI开发(一)相关推荐

  1. Android UI开发第二十五篇——分享一篇自定义的 Action Bar

    Action Bar是android3.0以后才引入的,主要是替代3.0以前的menu和tittle bar.在3.0之前是不能使用Action Bar功能的.这里引入了自定义的Action Bar, ...

  2. Android UI开发第三十篇——使用Fragment构建灵活的桌面

    http://www.lupaworld.com/article-222973-1.html 当我们设计应用程序时,希望能够尽最大限度的适配各种设备,包括4寸屏.7寸屏. 10寸屏等等,Android ...

  3. Android UI开发第四十一篇——墨迹天气3.0引导界面及动画实现

    周末升级了墨迹天气,看着引导界面做的不错,模仿一下,可能与原作者的代码实现不一样,但是实现的效果还是差不多的.先分享一篇以前的文章,android动画的基础知识,<Android UI开发第十二 ...

  4. Android UI开发第三十九篇——Tab界面实现汇总及比较

    Tab布局是iOS的经典布局,Android应用中也有大量应用,前面也写过Android中TAb的实现,<Android UI开发第十八篇--ActivityGroup实现tab功能>.这 ...

  5. Android 开发 -- 开发第一个安卓程序、Android UI开发(布局的创建:相对布局和线性布局、控件单位:px pt dp sp、常用控件 、常见对话框、ListView)

    文章目录 1. 开发第一个Hello World程序 1.1 开发程序 1.2 认识程序中的文件 1.3 Android程序结构 1.4 安卓程序打包 2. Android UI开发 2.1 布局的创 ...

  6. 【Android -- UI开发】一份 UI 开发学习指南

    思维导图 推荐资料:官方文档 六大布局 网上有人比喻的很好:布局好比是建筑里的框架,组件按照布局的要求依次排列,就组成了用于看见的漂亮界面了. 请看文章:[Android – UI 开发]六大布局 U ...

  7. Android UI开发——AppCompat实现Action Bar

    http://blog.csdn.net/xyz_lmn/article/details/12623609 每一位Android开发者对Action Bar这种设计都不陌生了,毕竟它已经发布了至少两年 ...

  8. Android UI开发第四篇——实现像handcent sms或者chomp sms那样的气泡短信样式

    今晚有点时间把断了很长时间的UI开发补一下,这次实现的是像handcent sms或者chomp sms那样的气泡短信样式,也是iphone上的气泡聊天模式.实现这种效果的重点是ListView的di ...

  9. Android UI开发第三十二篇——Creating a Navigation Drawer

    Navigation Drawer是从屏幕的左侧滑出,显示应用导航的视图.官方是这样定义的: The navigation drawer is a panel that displays the ap ...

  10. Android UI开发——Material Design界面设计【详细】

    转自:http://colachan.com/post/3416 找了很多Material Design的资料,终于找到一篇比较靠谱的.能看懂的,我认为非常有用的学习资料,就像他们说的,只要你按Mat ...

最新文章

  1. 微信人人商城云服务器,微信支付 · 微智人人商城使用文档 · 看云
  2. 51CTO完成B轮融资,围绕1400万社区用户的IT学习平台要怎么做?
  3. 2021年江西省副高考试成绩查询,江西省教育考试院:2021年江西高考成绩查询入口、查分系统...
  4. org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'xx' is defined
  5. Applese 的毒气炸弹
  6. 删除microsoft_如何从您的Microsoft帐户中删除设备
  7. 文字阴影-CSS Text-Shadow
  8. ruhr启动mysql数据库_Mysql表类型(存储引擎)的选择
  9. java 对 redis 的基本操作
  10. chardet java_java实现文件编码监测
  11. javascript Array类型 方法大全
  12. linux下的C语言开发(gdb调试)
  13. 中国结肠镜设备行业市场供需与战略研究报告
  14. hdu acm 1540
  15. kettle软件的使用
  16. macbook键盘失灵_如何使用MacBook的键盘在所有设备上键入
  17. pixi 小游戏_关于PIXI引擎制作页面小游戏的几个总结
  18. 图形编程丨图形绘制基础imgui篇—D3D9 HOOK 创建内部Imgui窗口
  19. 又一暴强的截图工具 ShareX
  20. PLSQL创建表空间的方法

热门文章

  1. FileZilla使用ssh服务和FTP服务连接Ubuntu
  2. Java开发工具下载地址
  3. @Before, @BeforeClass, @BeforeEach 和 @BeforeAll之间的不同
  4. Codeforces - Jeremy Bearimy
  5. 2022数据结构习题(知产)
  6. wp7开发实例:Baby Sign Language
  7. 压缩软件不同,如何删除压缩包密码
  8. 通用代码生成器,一键生成前端和后端
  9. CSS实现背景模糊效果(高斯模糊)
  10. 走过一片麦田,只能摘一次,并且不能回头,如何保证摘到的麦穗尽可能大