一、Android常用布局属性

1. LinearLayout的特有属性

android:orientation:设置布局排列方式
   android:layout_weight:设置所占布局的权重
   android:weightSum:设置最大权重和

2. RelativeLayout的特有属性

  • 属性值为“true”或“false”
    android:layout_centerHrizontal:位于父控件的横向中间位置
    android:layout_centerVertical:位于父控件的纵向中间位置
    android:layout_centerInparent:位于父控件的纵横向中间位置
    android:layout_alignParentBottom:贴紧父元素的下边缘
    android:layout_alignParentLeft:贴紧父元素的左边缘
    android:layout_alignParentRight:贴紧父元素的右边缘
    android:layout_alignParentTop:贴紧父元素的上边缘
    android:layout_alignParentStart:将控件开始位置与父控件的开始位置对齐
    android:layout_alignParentEnd:将控件结束位置与父控件的结束位置对齐

  • 属性值为引用“id”
    android:layout_below:在某元素的下方
    android:layout_above:在某元素的的上方
    android:layout_toLeftOf:在某元素的左边
    android:layout_toRightOf:在某元素的右边
    android:layout_toStartOf:在某元素的开始位置
    android:layout_toEndOf:在某元素的结束位置
    android:layout_alignTop:本元素的上边缘和某元素的的上边缘对齐
    android:layout_alignLeft:本元素的左边缘和某元素的的左边缘对齐
    android:layout_alignBottom:本元素的下边缘和某元素的的下边缘对齐
    android:layout_alignRight:本元素的右边缘和某元素的的右边缘对齐
    android:layout_alignStart:本元素与某元素开始位置对齐
    android:layout_alignEnd:本元素与某元素结束位置对齐
    android:layout_alignBaseline:将当前控件的基线与指定id控件t的基线对齐

3. 通用属性

  • padding(内边距)
    android:padding:设置本元素内容离本元素四边缘相同的距离
    android:paddingStart:本元素内容离本元素开始位置的距离
    android:paddingEnd:本元素内容离本元素结束位置的距离
    android:paddingLeft:本元素内容离本元素左边缘的距离
    android:paddingRight:本元素内容离本元素右边缘的距离
    android:paddingTop:本元素内容离本元素上边缘的距离
    android:paddingBottom: 本元素内容离本元素下边缘的距离

  • margin:(外边距)
    android:layout_margin:设置离某元素四边缘相同的距离
    android:layout_marginStart:离某元素开始位置的距离
    android:layout_marginEnd:离某元素结束位置的距离
    android:layout_marginLeft:离某元素左边缘的距离
    android:layout_marginRight:离某元素右边缘的距离
    android:layout_marginTop:离某元素上边缘的距离
    android:layout_marginBottom:离某元素底边缘的距离

  • 单个控件属性
    android:id:指定控件的ID
    android:width:设置控件的宽度
    android:maxWidth:设置控件的最大宽度
    android:minWidth:设置控件的最小宽度
    android:height:设置控件的高度
    android:maxHeight:设置控件的最大高度
    android:minHeight:设置控件的最小高度
    android:background:设置该控件的背景色
    android:text:设置控件中显示的文字
    android:textSize:设置控件中字体的大小
    android:textColor:设置控件中字体的颜色
    android:textStyle:设置控件中字体的样式
    android:grivity:设置控件的基本位置,针对控件里的元素来说
    android:layout_gravity:设置控件的基本位置,针对控件本身而言
    android:singleLine:设置单行显示
    android:drawableLeft:在text的左边输出一个drawable,可以是图片,样式,颜色等
    android:drawableRight:在text的右边输出一个drawable,可以是图片,样式,颜色等
    android:drawableTop:在text的上方输出一个drawable,可以是图片,样式,颜色等
    android:drawableBottom:在text的下方输出一个drawable,可以是图片,样式,颜色等
    android:drawablePadding:设置text与drawable的间隔
    android:ems:设置TextView的宽度为N个字符的宽度
    android:maxEms:设置TextView的宽度为最长为N个字符的宽度
    android:minEms:设置TextView的宽度为最短为N个字符的宽度
    android:maxLength:限制显示的文本长度,超出部分不显示
    android:lines:设置文本的行数
    android:maxLines:设置文本的最大显示行数
    android:minLines:设置文本的最小显示行数
    android:lineSpacingExtra:设置行间距
    android:lineSpacingMultiplier:设置行间距的倍数

4、公共属性
1) layout_width -宽
    fill_parent: 宽度和父元素相同,wrap_content: 宽度随本身的内容所调整,或者指定 px值来设置宽
2) layout_height - 高
    fill_parent: 高度和父元素相同,wrap_content: 高度随本身的内容所调整,或者指定 px值来设置高
3) background -设置背景图
4) padding -设置边距
    可以具体设置paddingBottom,paddingLeft,paddingRight,paddingTop来设定不同的px值
6) id -该object的id号
    @+id/id1 代表添加新的id名为id1, @id/id1 代表引用id1的控件
7) layout_gravity- Container组件的对齐方式
     组件在layout里面的对齐方式。
8) gravity-文字在组件里的对齐方式
    例如设置button里面的文字在button中居中显示。

二、Android常用控件

1、TextView.文本标签

     <TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:id="@+id/abc" />

2、EditView.编辑文本框
       主要函数:setText/getText设置/获取文本内容,setHint设置缺省显示内容;

    <EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/edit"
android:inputType="text"
android:hint="请在此输入用户名..."/> //hint就是灰色的那些提示字

3、Button.按钮

  <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:text="click me"/>

4、RadioButton.单选按钮  RadioGroup

RadioButton的isChecked()判断是否被选中
获取选中RadioButon的ID:设置RadioGroup.setOnCheckedChangeListener方法
public onCheckedChanged(RadioGroup group,int checkedId)//checkedId是选中RadioButton的ID

 <RadioGroup
android:id="@+id/group1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/a1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="aaa"/> //单标签,后面有/
<RadioButton
android:id="@+id/a2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="bbb"/>
</RadioGroup>
RadioButton:要注意的是因为单选按钮需要分组,也就是你得分好哪几个单选按钮之间只能选一个,然后用RadioGroup标签来圈起来

5、CheckBox多选框
isChecked()判断是否被选中
setOnCheckedChangeListener方法监视选中状态改变情况

  <CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/checkbox1"
android:text="aaa"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/checkbox2"
android:text="bbb"/>

6、ImageView/ImageButton.图片

setImageResource 设置显示图片
setAlpha 设置Alpha
invalidate 强制更新
setScaleType( ScaleType st) 设置图片显示方式,st是一枚举
setAdjustViewBounds 设置是否调整控件适应图片大小
setBackgroundResource 设置背景

  <ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/image"
android:src="@drawable/ic_launcher"/>
要注意的是其一般是通过src属性引用res-drawable对应分辨率的文件夹、assets或网络等地方的图片

7、Spinner
    a,显示数据
1),硬编码加载
通过setAdapter方法设置类型为ArrayAdapter (Context context, int textViewResId, String []objects)
   textViewResourceId:显示内容的ViewID默认设置为R.layout.simple_spinner_item
   objects:显示的内容
2),从资源文件中加载
ArrayAdapter.createFromResource (Context context, int textArrayResId, int textViewResId) //textArrayResId是资源ID
返回ArrayAdapter<CharSequence>
    b,设置下拉列表的风格(可选)
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    c,监听选项选择变更事件
setOnItemSelectedListener设置监听去
Spinner.OnItemSelectedListener类须实现以下两个方法
public void onItemSelected (AdapterView<?> parent, View view, int position, long id)//view 选中项实例,position选择项在adapter中的位置
public void onNothingSelected(AdapterView<?> arg0)
    d,设置选中项
Spinner.setSelection(position)//索引从0开始
    d,获取选中项
getSelectedItemPosition ()
getSelectedItem () //该值toString()则为选中内容的字符串
getSelectedItemId ()
getSelectedView ()

8、AutoCompleteTextView
1,setAdapter设置数据adapter
2,设置输入多少个字符显示提示AutoCompleteTextView.setThreshold(int threshold)

9、MultiAutoCompleteTextView (允许输入多值,多值之间会自动地用指定的分隔符分开)
1,setAdapter设置数据adapter
2,setTokenizer设置提示Tokenizer缺省的为new MultiAutoCompleteTextView.CommaTokenizer()以逗号分隔

10、DatePicker/TimePicker
a,DatePicker
1),设置初始显示日期init(int Year, int month, int day, new DatePicker.OnDateChangedListener(){
    public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth){}
   })
2),获取设置值 getYeah()/getMonth()/getDayOfMonth()
b,TimePicker
1),设置setCurrentHour (Integer currentHour) /setCurrentMinuter (Integer currentHour)
2),setIs24HourView(Boolean)设置是否为24小时制显示
3),监听设置改变setOnTimeChangedListener
c,获取系统当期时间和日期
Calendar.getInstance()返回Calendar
Calendar.get (int field) ---field 可以为Calendar.YEAR/ Calendar.MONTH/ Calendar.DAY_OF_MONTH/ Calendar.HOUR_OF_DAY/ Calendar.MINUTE

11、Gallery(显示一系列的图片,提供拖动等特效)
     显示数据
   setAdapter(SpinnerAdapter adapter)设置数据适配器.
数据适配器可以继承自BaseAdapter,该类 public View getView(int position, View convertView, ViewGroup parent) 返回当前选择的视图(ImageView)
     选项选中监听setOnItemSelectedListener

12、GridView(表格显示一系列图片)
     显示数据
    setAdapter(ListAdapter adapter)设置数据适配器.
数据适配器可以继承自BaseAdapter,该类 public View getView(int position, View convertView, ViewGroup parent) 返回当前选择的视图(ImageView)
     选项选中监听setOnItemSelectedListener
     getSelectedView()返回当前选中的视图

13、ProgressBar
setIndeterminate 设置进度条是否自动运转
setProgressStyle 设置显示风格.ProgressDialog.STYLE_HORIZONTAL/ProgressDialog.STYLE_SPINNER
setProgress 设置进度
setMax 设置最大值
getProgress()获取当前进度

14、SeekBar
方法
setMax 设置最大值
getProgress()获取当前值
setProgress 设置值
setIndeterminate 监听器
setOnSeekBarChangeListener其下有三个方法
   public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) //数值变更,arg1新值,是否Touch
   public void onStartTrackingTouch(SeekBar seekBar)//开始拖动
   public void onStopTrackingTouch(SeekBar seekBar) //结束拖动

15、ListView
      a,显示数据setAdapter(ListAdapter adapter)
      b,动态增删数据
        adapter.notifyDataSetChanged()
      d,设置背景色
        setBackGroudRource

Android常用布局和控件的更多相关文章

  1. Android常用酷炫控件(开源项目)github地址汇总

    转载一个很牛逼的控件收集帖... 第一部分 个性化控件(View) 主要介绍那些不错个性化的 View,包括 ListView.ActionBar.Menu.ViewPager.Gallery.Gri ...

  2. Android 常用炫酷控件(开源项目)git地址汇总

    第一部分 个性化控件(View) 主要介绍那些不错个性化的 View,包括 ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView.P ...

  3. 让我们创建屏幕- Android UI布局和控件

    下载LifeCycleTest.zip - 278.9 KB 下载ViewAndLayoutLessons_-_Base.zip - 1.2 MB 下载ViewAndLayoutLessons_-_C ...

  4. Android 简单布局、控件

    布局 线性布局 LinearLayout <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android ...

  5. Android学习之基础知识五—Android常用的七大控件

    一.TextView控件:在界面上显示一段文本信息 先看XML代码和执行效果:         代码分析: 1.android:id属性,给当前控件定义了一个唯一的标识符 2.android:layo ...

  6. Android常用框架和控件使用

    Router框架 https://github.com/iqiyi/Andromeda/blob/master/CHINESE_README.md https://github.com/alibaba ...

  7. Android开发之基本控件和详解四种布局方式

    Android中的控件的使用方式和iOS中控件的使用方式基本相同,都是事件驱动.给控件添加事件也有接口回调和委托代理的方式.今天这篇博客就总结一下Android中常用的基本控件以及布局方式.说到布局方 ...

  8. 【读书笔记《Android游戏编程之从零开始》】3.Android 游戏开发常用的系统控件(Button、Layout、ImageButton)

    3.1 Button Button这控件不用多说,就是一个按钮,主要是点击后进行相应事件的响应. 给组件添加ID属性:定义格式为 android:id="@+id/name",这里 ...

  9. Android布局及控件的属性说明

    android常用的一些属性说明   android:id --- 为控件指定相应的ID android:text --- 指定控件当中显示的文字,需要注意的是,这里尽量使用strings.xml文件 ...

随机推荐

  1. cmder安装(window下好用的终端)

    cmder下载地址:cmder官网 安装步骤: 下载cmder(cmder官网)并解压缩 配置环境变量 先配置CMDER_HOME(cmder.exe所在目录) 配置path:%CMDER_HOME% ...

  2. How to change the text color in the terminal

    You can open the file ~/.bashrc and then choose the force_color_prompt=yes otherwise, you can change ...

  3. bzoj2023[Usaco2005 Nov]Ant Counting 数蚂蚁*&&bzoj1630[Usaco2007 Demo]Ant Counting*

    bzoj2023[Usaco2005 Nov]Ant Counting 数蚂蚁&&bzoj1630[Usaco2007 Demo]Ant Counting 题意: t个族群,每个族群有 ...

  4. Python Ethical Hacking - Malware Analysis(2)

    Filtering Command Output using Regex #!/usr/bin/env python import smtplib import subprocess import r ...

  5. win7下建立docker共享文件夹

    前言 建立本机(win7)和VirtualBox中docker虚拟机的共享文件夹,注:下面的命令都是以root身份运行的,使用sudo -i切换到root身份,如无法切换,请自行在命令前加上sudo命 ...

  6. maven&nexus_repository 私库搭建与使用

    一.nexus仓库安装 1,http://www.sonatype.org/nexus/    下载sso版本,免费2,tar -zxvf nexus-2.11.1-01-bundle.tar.gz3 ...

  7. 题解 UVA501 【Black Box】

    思路与中位数一题,解决方案比较像,使用对顶堆来解决. 具体实现为,使用两个堆,大根堆维护较小的值,小根堆维护较大的值,即小根堆的堆顶是较大的数中最小的,大根堆的堆顶是较小的数中最大的. 将大于大根堆堆 ...

  8. 占个坑 未来学qt的时候专用

    今天看了一个大佬发了一个上位机图片便向大佬问道 ”上位机是用什么软件做的“大佬抛下一句qt ,在业界内很通用,windows和linux通吃,便让我萌生了一个想法,去学qt.虽说上位机时常听到,但是自 ...

  9. Java应用服务器之tomcat会话复制集群配置

    会话是识别用户,跟踪用户访问行为的一个手段,通过cookie(存在客户端)或session(存在服务端)来判断本次请求是那个客户端发送过来:常用的会话保持有绑定会话,就是前边我们聊的在代理上通过算法或 ...

  10. DJANGO-天天生鲜项目从0到1-008-列表页

    本项目基于B站UP主‘神奇的老黄’的教学视频‘天天生鲜Django项目’,视频讲的非常好,推荐新手观看学习 https://www.bilibili.com/video/BV1vt41147K8?p= ...