1.LinearLayout (线性布局)

 它是一个视图组,所有的子元素都是单个方向的:

vertically(垂直) or horizontally(水平),要么都是垂直,要么都是水平。

属性(attribute):android:orientation 。

属性:android:layout_weight="1".

Google官方推荐,当使用weight属性时,将width设为0dip即可,效果跟设成wrap_content是一样的。这样weight就可以理解为占比了!

layout_width="wrap_content"只是在子LinearLayout布局下使用,并且有的也建议改成0dp.

(1)新建项目-androidLinearLayout

(2)修改layout的布局文件activity_main.xml:

   默认下的布局是RelativeLayout,(相对布局),

   然后就改成LinearLayout.

(3)编写一个实例-用户登录界面(采用LinearLayout布局)

效果图:

设计界面(可视化界面):

2019-08-20 Android 线性布局介绍-LinearLayout_第1张图片

AVD上的效果图:

2019-08-20 Android 线性布局介绍-LinearLayout_第2张图片

 思路:从大到小的布局,均采用LinearLayout线性布局。

              再添加控件,进行里面的细节修改。

layout的布局文件activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical" #大布局-垂直

    tools:context="com.example.andriod_linearlayout.MainActivity" >


   

    android:layout_width="match_parent"

    android:layout_height="wrap_content"  #根据内容设置

    android:background="#CCCCCC" #背景颜色

    android:orientation="horizontal">  #小布局-水平

       

            android:id="@+id/textView1"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="@string/username" /> #在values 的strings.xml下定义,消除下警告(黄色波浪线)

       

            android:id="@+id/editText1"

            android:layout_width="0dp"(消除黄色波浪线)

            android:layout_height="wrap_content"

            android:layout_weight="1"(“权重”)

            android:ems="10" >

           

       



   

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:background="#CCCCCC"

    android:orientation="horizontal">

       

            android:id="@+id/textView2"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

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

       

            android:id="@+id/editText2"

            android:layout_width="0dp"

            android:layout_height="wrap_content"

            android:layout_weight="0.76"

            android:ems="10"

            android:inputType="textPassword" />

       


 

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:background="#CCCCCC"

    android:gravity="center"

    android:orientation="horizontal">

     

          android:id="@+id/button1"

          android:layout_width="wrap_content"

          android:layout_height="wrap_content"

          android:layout_weight="1"#权重 

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

     

          android:id="@+id/button2"

          android:layout_width="wrap_content"

          android:layout_height="wrap_content"

          android:layout_weight="1"

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


     


strings.xml 文件:

<?xml version="1.0" encoding="utf-8"?>

    andriod_linearlayout

    Hello world!

    Settings

    用户名:

    密码:

    登录

    取消

更多相关文章

  1. Android相对属性布局总结
  2. android android:windowSoftInputMode相关属性
  3. Android布局优化之merge(二)
  4. Android 属性系统设计分析
  5. [读书笔记]布局的屏幕适配常用方法
  6. Android布局优化之ViewStub(三)
  7. Android属性动画——实现灵动菜单效果
  8. Android 属性动画ValueAnimator和ObjectAnimator的高级用法

随机推荐

  1. Linux 之 crontab定时任务操作过程2016-1
  2. Linux比较两个文件之间的不同
  3. 【Ubuntu手记】开发多线程程序时在eclips
  4. Linux操作系统的I2C驱动
  5. linux查看硬件信息及驱动设备相关整理
  6. xshell连接linux与windows连接与传文件
  7. Linux进程通信[2]-互斥锁和条件变量
  8. linux运行命令缺少依赖库的查找方法
  9. 64位win 7装Linux虚拟机(RedHat)
  10. 看谁能找出bug★☆open函数总是返回-1