android布局之Linearlayout(线性布局)

来源:互联网 发布:c语言实战项目开发 编辑:程序博客网 时间:2024/06/06 03:28

android布局中的Linearlayout(线性布局)布局是较为常见的一种布局,正如它的名字,在此布局里的控件将按照线性排布,要么横向,要么垂直。文字的说明太难理解,还是看看实际代码吧:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_main2"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal"    tools:context="com.example.myche.Main2Activity">    <Button        android:text="1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/button2"        android:layout_weight="1" />    <Button        android:text="2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/button3"        android:layout_weight="1" />    <Button        android:text="3"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/button"        android:layout_weight="1" /></LinearLayout>


这便是一个线性布局了,我在这个布局里放了三个按钮,用来看出它的布局效果,布局大小不用了说了,这里最为重要的一个属性,那就是 android:orientation,这个属性用来指定这个布局是横向还是垂直, android:orientation="horizontal"这个属性便表示布局为横向:  




而当我们把这个属性改为 android:orientation="vertical"时,它便成立垂直排列


以上两种便是其核心了,而根据你所要的效果,你可以在垂直的线性布局中嵌套横向的线性布局来实现诸如九宫格的布局,等等,靠灵活的组合可以做出各式各样的显示效果

原创粉丝点击