LinearLayout:线性布局的控件,要么横向排布要么纵向排布 无论是哪种排布,都由android:orientation来控制,其中vertical是横向,Horizaontal是纵向 常用的属性: android:gravity————设置的是控件自身上面的内容位置
android:layout_gravity—–设置控件本身相对于父控件的显示位置
android:layout_weight—– 给控件分配剩余空间
代码举例: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent"//宽度填满整个空间 android:layout_height="match_parent"空间//高度填满 android:baselineAligned="false" android:orientation="vertical">//横向排布 <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="按钮1" />//生成了一个按钮 </LinearLayout>顺便清楚一下其中的fill_parent,wrap_content和match_parent之间的区别 1)fill_parent是设置一个顶部布局或控件强制性让它布满整个屏幕 2)wrap_content布局指根据视图内部内容自动扩展以适应其大小。 3)match_parent和fill_parent的用法一样
代码举例: wrap_content: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:baselineAligned="false" android:orientation="vertical"> <Botton android:id="@+id/btnButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button CJP"/> </RelativeLayout>