LinearLayout(线性布局)

来源:互联网 发布:.tm商标域名 编辑:程序博客网 时间:2024/06/05 20:35

  LinearLayout初学者练习



  LinearLayout线性布局):布局,就是把控件按照某种特定的方式排序,控件之间的关系独立的。而线性布局分为两个方向:横向(Android:orientation="horizontal")或纵向(android:orientation="vertical").

1.

android布局--Android fill_parent、wrap_content和match_parent的区别

三个属性都用来适应视图的水平或垂直大小,一个以视图的内容或尺寸为基础的布局比精确地指定视图范围更加方便。

1)fill_parent

设置一个构件的布局为fill_parent将强制性地使构件扩展,以填充布局单元内尽可能多的空间。这跟Windows控件的dockstyle属性大体一致。设置一个顶部布局或控件为fill_parent将强制性让它布满整个屏幕。

2) wrap_content

设置一个视图的尺寸为wrap_content将强制性地使视图扩展以显示全部内容。以TextView和ImageView控件为例,设置为wrap_content将完整显示其内部的文本和图像。布局元素将根据内容更改大小。设置一个视图的尺寸为wrap_content大体等同于设置Windows控件的Autosize属性为True。

3)match_parent
   Android2.2中match_parent和fill_parent是一个意思 .两个参数意思一样,match_parent更贴切,于是从2.2开始两个词都可以用。那么如果考虑低版本的使用情况你就需要用fill_parent了

//转自:http://www.cnblogs.com/nikyxxx/archive/2012/06/15/2551390.html。


2.    0dip的用法

    布局中常见:layout_height="0dip"  或者 layout_width="0dip"   

     这里的意思是不是用具体的数值来计算高度或者宽度,而是和layout_weight 一起来使用的。经常会有这样的写法: 同一个LinearLayout  (假设是横向的) 中 有几个view ,但是都不指定其具体的宽度,而是使用 layout_weight 参数来指定这几个view所占的比例。每个view所占的宽度的比例是自己的layout_weight 值和总的layout_weight的总和的比值。

例如:

<LinearLayoutandroid:id="@+id/remove_account_area"

       android:layout_width="match_parent"

       android:layout_height="wrap_content"

       android:orientation="horizontal"

       android:background="@android:drawable/bottom_bar">

        <View

           android:layout_width="0dip"

           android:layout_height="match_parent"

           android:layout_weight="1"/>

        <Buttonandroid:id="@+id/remove_account_button"

           android:layout_width="0dip"

           android:layout_height="wrap_content"

            android:layout_weight="2"

           android:layout_marginTop="5dip"

           android:text="@string/remove_account_label" />

        <View

            android:layout_width="0dip"

           android:layout_height="match_parent"

           android:layout_weight="1"/>

   </LinearLayout>

这几个View宽度的的比例便是1:2:1

//转自:http://www.xuebuyuan.com/1111975.html。

3.


模仿设计小米计算机页面布局:



红色标记的是注意点


<?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" >

    <LinearLayout

        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >//注意括号的位置

        <Button
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="c"
            android:textColor="#FCD648"/>         //设置标题颜色
        


        <Button
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="DEL"/>
        

        <Button
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="÷" />

        <Button
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="×" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <Button
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="7" />

        <Button
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="8" />

        <Button
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="9" />

        <Button
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="-" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <Button
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="4" />

        <Button
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="5" />

        <Button
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="6" />

        <Button
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="+" />
    </LinearLayout>

    <LinearLayout
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <LinearLayout
        android:orientation="vertical"     
        android:layout_width="0dip"
        android:layout_weight="3"
        android:layout_height="wrap_content">

            <LinearLayout
                         android:layout_width="fill_parent"
                         android:layout_height="wrap_content"
                         android:orientation="horizontal">

                <Button
                    android:layout_width="0dip"
                   android:layout_weight="1"
                    android:layout_height="wrap_content"
                   
                    android:text="1" />

                <Button
                    android:layout_width="0dip"
                     android:layout_weight="1"
                    android:layout_height="wrap_content"
                   
                    android:text="2" />

                <Button
                    android:layout_width="0dip"
                       android:layout_weight="1"
                    android:layout_height="wrap_content"
                 
                    android:text="3" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" >

                <Button
                    android:layout_width="0dip"
                    android:layout_weight="2"
                    android:layout_height="wrap_content"
          
                    android:text="0" />

                <Button
                    android:layout_width="0dip"
                     android:layout_weight="1"
                    android:layout_height="wrap_content"
                   
                    android:text="." />
        
            </LinearLayout>
            
           
        </LinearLayout>

        <Button
             android:text="="
            android:layout_width="0dip"
            android:layout_weight="1"
            android:layout_height="fill_parent"
           
            android:background="#FCD648"  //设置背景
            android:gravity="bottom|right"、、//设置标题居右下
            
          
           
        />
        
        
        
        
        
    </LinearLayout>
        
        
        
        
        
        
        
        

    </LinearLayout>

效果图:






1 0
原创粉丝点击