android LinearLayout 安卓无忧第一篇

java学习手册,最全java资料


今天给大家介绍一下安卓布局中的 LinearLayout,文章分为四个部分

 线性布局是一个很常用的布局,可以让它的子元素垂直或水平的方式排成一行,但是在我的工作中只在布局结构简单的时候使用,很多时候是使用相对布局的。因为如果布局结构复杂时使用线性布局往往需要LinearLayout嵌套LinearLayout,而这会使得加载页面的效率变低。所以写布局文件之前要好好分析需求,再决定是否使用LinearLayout。

1 常用属性

android:orientation

android:orientation="vertical"垂直线性布局,"horizontal"水平线性布局。

android:gravity="top"(buttom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizontal、center、fill、clip_vertical、clip_horizontal)控制布局中控件的对齐方式。如果是没有子控件的控件设置此属性,表示其内容的对齐方式,比如说TextView里面文字的对齐方式;若是有子控件的控件设置此属性,则表示其子控件的对齐方式,gravity如果需要设置多个属性值,需要使用“|”进行组合

android:baselineAligned

默认为true,当android:orientation=“horizontal ”时,其子View的baseline 是对齐的,表现在不同View(TextView、Button、EditView)显示的文字的baseline是对齐的。

效果:上排为true时的效果


android:divider

在每个子View间加分隔线。可设置为颜色值(#rgb","#argb", "#rrggbb", or "#aarrggbb")或资源引用

“@[+][package:]type:name" 或属性引用"?[package:][type:]name",需android:showDividers不为none时才会显示

android:showDividers默认为none,设置分割线显示位置,可选项包含:none、beginning、middle、end。其中middle较常用

效果:此为middle的效果



2 gravity 与layout_gravity的区别

android:gravity是指定本元素的子元素相对它的对齐方式。

android:layout_gravity是指定本元素相对它的父元素的对齐方式。

3 android:layout_weight

在android开发中LinearLayout很常用,LinearLayout的内控件的android:layout_weight在某些场景显得非常重要,比如我们需要按比例显示。android并没用提供table这样的控件,虽然有TableLayout,但是它并非是我们想象中的像html里面的table那么好用,我们常用ListView实现table的效果,但是列对齐确比较麻烦,现在用LinearLayout及属性android:layout_weight能很好地解决。下面我们共同体验下layout_weight这个属性。

  LinearLayout内的控件的layout_width设置为"wrap_content",效果如下:


可以看到这三个TextView是按照1:2:3的比例进行显示的,这样看来似乎可以实现按照比例显示了,但是有个问题,如果TextView内的文本长度一同那么较长文本的TextView会宽度会有所增加,见下面配置及效果:

配置如下:

<LinearLayout
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:layout_width="fill_parent"
    android:orientation="horizontal">
<TextView
        android:background="#aa0000"
        android:gravity="center"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:text="1"/>

<TextView
        android:background="#00aa00"
        android:gravity="center"
        android:layout_height="fill_parent"
        android:layout_weight="2"
        android:layout_width="wrap_content"
        android:text="1"/>

<TextView
        android:background="#0000aa"
        android:gravity="center"
        android:layout_height="fill_parent"
        android:layout_weight="3"
        android:layout_width="wrap_content"
        android:text="1"/>
</LinearLayout>

效果如下:


这样看来我们所需要的按比例又无法实现了。

只有LinearLayut中子view将layout_width设置为"0dp"。配置及效果见下:

<LinearLayout
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:layout_width="fill_parent"
    android:orientation="horizontal">
<TextView
        android:background="#aa0000"
        android:gravity="center"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:text="1111111111111111111111111111111111111111111"/>

<TextView

        android:background="#00aa00"
        android:gravity="center"
        android:layout_height="fill_parent"
        android:layout_weight="2"
        android:layout_width="0dp"
        android:text="2"/>

<TextView
        android:background="#0000aa"
        android:gravity="center"
        android:layout_height="fill_parent"
        android:layout_weight="3"
        android:layout_width="0dp"
        android:text="3"/>

</LinearLayout>

效果如下,这样终于达到我们的按比例显示的效果了。 


4 安卓无忧例子

首先打开安卓无忧,界面如下,


进入布局管理,点击LinearLayout,界面如下:






点击源码查看源码界面如下




最后,请去应用宝下载安卓无忧,源码例子文档一网打尽:源码下载。百度网盘地址:http://yun.baidu.com/share/link?shareid=996618543&uk=1000858045

本人微博:honey_11



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章