Android的布局控件----LinearLayout(线性布局)
2016-04-11 16:31
696 查看
在Android中,常见的布局控件包括LinearLayout(线性布局)、RelativeLayout(相对布局)、TableLayout(网格布局)、FrameLayout(帧布局)、AbsoluteLayout(绝对布局)等,其中最常用的是:LinearLayout(线性布局)、RelativeLayout(相对布局)。
LinearLayout布局是Android中应用最广泛的,也是最基础的一种布局文件。LinearLayout布局可以使放入其中的组件以水平方式或者垂直的方式整齐排列,LinearLayout的继承关系如下:
属性:android:layout_width属性定义了控件的宽度;android:layout_height属性定义了控件的高度,anroid:layout_weight:属性设置了每个组件在布局中所占的比重(权重),android:orientation属性指定具体的排列方式。android:orientation的属性值为horizontal表示组件以水平方式排列,android:orientation的属性值为vertical表示组件以垂直方式排列。下面使用LineraLayout布局例子如下:
main.xml如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#aaaaaa"
>
<!-- 顶部 -->
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
>
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#00ff00"
android:text="微信"
android:textColor="#ffffff"
android:textSize="26sp" />
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ff00ff"
android:text="支付宝"
android:textColor="#ffffff"
android:textSize="26sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1"
>
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0000"
android:gravity="center"
android:text="淘宝"
android:textColor="#ffffff"
android:textSize="26sp" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
>
<Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#00BFFF"
android:text="钉钉"
android:textSize="20sp"
android:textColor="#ffffff"
/>
<Button
android:id="@+id/button5"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#DC143C"
android:text="QQ"
android:textSize="20sp"
android:textColor="#ffffff"
/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
>
<Button
android:id="@+id/button6"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#FFD700"
android:text="UC浏览器"
android:textColor="#ffffff"
android:textSize="20sp"
/>
<Button
android:id="@+id/button7"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#7FFF00"
android:text="微博"
android:textColor="#ffffff"
android:textSize="20sp"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
>
<Button
android:id="@+id/button8"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ff4500"
android:text="百度"
android:textColor="#ffffff"
android:textSize="26sp" />
<Button
android:id="@+id/button9"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#00ffff"
android:text="京东"
android:textColor="#ffffff"
android:textSize="26sp" />
</LinearLayout>
</LinearLayout>
运行效果如下:
最后说几句:LinearLayout布局可使用嵌套。活用LinearLayout布局,可以设计出各种各样漂亮的布局方式。
LinearLayout布局是Android中应用最广泛的,也是最基础的一种布局文件。LinearLayout布局可以使放入其中的组件以水平方式或者垂直的方式整齐排列,LinearLayout的继承关系如下:
java.lang.Object | |||
↳ | android.view.View | ||
↳ | android.view.ViewGroup | ||
↳ | android.widget.LinearLayout |
main.xml如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#aaaaaa"
>
<!-- 顶部 -->
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
>
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#00ff00"
android:text="微信"
android:textColor="#ffffff"
android:textSize="26sp" />
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ff00ff"
android:text="支付宝"
android:textColor="#ffffff"
android:textSize="26sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1"
>
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0000"
android:gravity="center"
android:text="淘宝"
android:textColor="#ffffff"
android:textSize="26sp" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
>
<Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#00BFFF"
android:text="钉钉"
android:textSize="20sp"
android:textColor="#ffffff"
/>
<Button
android:id="@+id/button5"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#DC143C"
android:text="QQ"
android:textSize="20sp"
android:textColor="#ffffff"
/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
>
<Button
android:id="@+id/button6"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#FFD700"
android:text="UC浏览器"
android:textColor="#ffffff"
android:textSize="20sp"
/>
<Button
android:id="@+id/button7"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#7FFF00"
android:text="微博"
android:textColor="#ffffff"
android:textSize="20sp"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
>
<Button
android:id="@+id/button8"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ff4500"
android:text="百度"
android:textColor="#ffffff"
android:textSize="26sp" />
<Button
android:id="@+id/button9"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#00ffff"
android:text="京东"
android:textColor="#ffffff"
android:textSize="26sp" />
</LinearLayout>
</LinearLayout>
运行效果如下:
最后说几句:LinearLayout布局可使用嵌套。活用LinearLayout布局,可以设计出各种各样漂亮的布局方式。
相关文章推荐
- 利用power_profile.xml获取Android运行时的能耗
- 解决android 软键盘在全屏下设置adjustResize无效的问题
- Android PopupWindow详解
- Android Bundle详解
- Android中的PopupWindow详解
- TextView的ClickSpan
- volley框架的使用
- Android OpenGL ES 2.0 屏幕坐标和3D世界坐标转换
- android 通知栏显示下载进度
- Android工具类
- Android开发之Pending Intent
- android 自定义AlertDialog
- 广播接收器 broadcast sendOrderedBroadcast android
- Android动态换肤(二、apk免安装插件方式)
- Android动态换肤(二、apk免安装插件方式)
- android apk应用程序如何用代码实现 自己安装自己(自升级)?
- Android自定义下拉刷新动画--仿百度外卖下拉刷新
- android PorterDuffXfermode
- android之官方下拉刷新组件SwipeRefreshLayout
- Android按返回键退出程序但不销毁代码