- 版权声明:欢迎转载,转载请注明出处。 如果本文帮助到你,本人不胜荣幸,如果浪费了你的时间,本人深感抱歉。如果有什么错误,请一定指出,以免误导大家、也误导我。感谢关注。
转载请标明出处:http://www.jianshu.com/p/1bc5383a5e91
本文出自:CodeSaid的简书
什么是布局?
布局是一种可以放置很多控件的容器,他可以按照一定的规律调整内部控件的位置,从而编写出好看的界面。布局的内部也能够放置布局,形成多层嵌套。
线性布局 LinearLayout
LinearLayout 叫做线性布局,他会将其内部的控件在线性的方向依次排列。 他是按照属性android:orientation的指定,来决定其子视图设置为水平还是垂直。
- horizontal 表示一行 (水平) 默认的值
- vertical 表示一列 (垂直)
LinearLayout 是布局中的根视图,因此应将宽度和高度设置为 “match_parent”,从而填满可供应用使用的整个屏幕区域。
栗子:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_gravity="top"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1" />
<Button
android:layout_gravity="bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2"/>
</LinearLayout>
android:layout_gravity属性:指定控件在布局中对齐方式,需要根据父视图的排列方式来指定,比如父视图对齐方式为vertical,只有在水平方向上的对齐方式才有效
然后就是LinearLayout的重