安卓笔记1-LinearLayout(线性布局)

来源:互联网 发布:wps有mac版本吗 编辑:程序博客网 时间:2024/05/21 13:57

LinearLayout 又称作线性布局,这个布局会将它所包含的控件在线性方向上依次排列。

orientation 属性

android:orientation 属性指定了排列的方向
- horizontal 是指左右排列 (此时控件的宽度不能指定为 match_parent ,因为这样的话单独一个控件就会将整个水平方向占满,其他的控件没有可以放置的位置。)
- vertical 是指上下排列 (控件高度不能设置为 match_parent )

weight(权重)属性

这个属性允许我们使用比例的方式来指定控件的大小,它在手机屏幕的适配性方面可以起到非常重要的作用。

image

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/LinearLayout1"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal">    <LinearLayout        android:layout_width="0dp"        android:layout_height="fill_parent"        android:background="#ADFF2F"        android:layout_weight="1"/>    <LinearLayout        android:layout_width="0dp"        android:layout_height="fill_parent"        android:background="#DA70D6"        android:layout_weight="2"/></LinearLayout>

按比例划分水平方向:将涉及到的View的android:width属性设置为0dp,然后设置为android weight属性设置比例即可;类推,竖直方向,只需设android:height为0dp,然后设weight属性即可!

上下结构的代码:

image

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/LinearLayout1"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical">    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="button1"        android:layout_weight="1" />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="button2"        android:layout_weight="2"        android:id="@+id/button2" />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="button3"        android:layout_weight="3" /></LinearLayout>

原理:系统会先把 LinearLayout 下所有的控件指定的 layout_weight 值想家,得到一个总值,然后每个控件所占大小的比例就是用该控件的 layout_weight 值除以刚才计算的总值。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal" >    <EditText        android:id="@+id/input_message"        android:layout_width="0dp"        android:layout_height="wrap_content"        android:layout_weight="1"        android:hint="Type something"/>    <Button        android:id="@+id/send"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="send"/></LinearLayout>

我们通过指定部分控件的 layout_weight 值,来实现更好的效果。在上面的例子中,我们仅指定了 EditText 的 android:layout_weight 属性,并将 Button 的宽度改会 wrap_content 。这边是 Button 的跨度仍然按照 wrap_content 来计算,而 EditText 则会占满屏幕所有的剩余控件。

layout_gravity 属性

layout_gravity 是用于指定控件在布局中的对齐方式。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal" >    <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="top"        android:text="Button 1" />    <Button        android:id="@+id/button2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center_vertical"        android:text="Button 2" />    <Button        android:id="@+id/button3"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="bottom"        android:text="Button 3" /></LinearLayout>

由于目前 LinearLayout 的排列方向是 horizontal ,因此我们只能指定垂直方向上的排列方向,将第一个 Button 的对齐方式指定为 top ,第二个 Button 的对齐方式指定为 center_vertical ,第三个 Button 的对齐方式指定为 bottom。

by web开发者 from http://weber.pub/

本文地址: http://weber.pub/安卓笔记1-linearlayout(线性布局)/274.html

0 0