嵌入式之路,贵在日常点滴
---阿杰在线送代码
目录
六、 基础控件之Button,TextView,EditText,ImageView
八、做出一个智能家居布局图(新大陆2016年物联网国赛题目)
LinearLayout(线性布局)常用到的属性简单归纳一下:
下面涉及到两个主要的文件类型:
.xml负责页面布局效果,.java负责后台逻辑。
一、布局的种类
二、布局和页面的关系
运行结果:#ff0000红
整体框架
<RelativeLayout > //父布局
//往该布局添加控件
<TextView />
<Button/>
</RelativeLayout>
三、显示一张美女图
控件的宽度和高度
控件的宽度
android:layout_width="match_parent"
fill_parent和match_parent 都是大小跟父控件对齐
wrap_content 该控件有多大就显示多大
控件的高度
android:layout_height="match_parent"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg"
tools:context=".MainActivity" >
</RelativeLayout>
运行结果:
每个框架都有这三句话
四、布局背景颜色,背景图,显示两个美女
关于控件ID
给控件起一个ID:
android:id="@+id/girl1"
让一个控件和跟其他控件起联系,比如放它下面(就利用到这个ID):
android:layout_below="@id/girl1"
运行结果:
一个大布局底下有两个小布局
五、常用布局之相对布局
RelativeLayout中子控件常用属性
1、相对于父控件,例如:android:layout_alignParentTop=“true”
android:layout_alignParentTop 控件的顶部与父控件的顶部对齐;
android:layout_alignParentBottom 控件的底部与父控件的底部对齐;
android:layout_alignParentLeft 控件的左部与父控件的左部对齐;
android:layout_alignParentRight 控件的右部与父控件的右部对齐;
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<RelativeLayout
android:id="@+id/girl2"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/testpic32"
></RelativeLayout>
</RelativeLayout>
运行结果:
2、相对给定Id控件,例如:android:layout_above=“@id/**”
android:layout_above 控件的底部置于给定ID的控件之上;
android:layout_below 控件的底部置于给定ID的控件之下;
android:layout_toLeftOf 控件的右边缘与给定ID的控件左边缘对齐;
android:layout_toRightOf 控件的左边缘与给定ID的控件右边缘对齐;
android:layout_alignBaseline 控件的baseline与给定ID的baseline对齐;(少用)
android:layout_alignTop 控件的顶部边缘与给定ID的顶部边缘对齐;
android:layout_alignBottom 控件的底部边缘与给定ID的底部边缘对齐;
android:layout_alignLeft 控件的左边缘与给定ID的左边缘对齐;
android:layout_alignRight 控件的右边缘与给定ID的右边缘对齐;
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<RelativeLayout
android:id="@+id/girl2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/testpic32"
></RelativeLayout>
<RelativeLayout
android:id="@+id/girl1"
android:layout_toRightOf="@id/girl2"
android:layout_alignBottom="@id/girl2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/testpic"
></RelativeLayout>
</RelativeLayout>
运行结果: