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. 词云图的几种制作方法评测,你pick哪款
  2. 动画:用动画给女朋友讲解 TCP 四次分手过
  3. 浅谈集群版Redis和Gossip协议
  4. 什么!Python还能帮你找老婆?
  5. 用Python唱一首程序员版“惊雷”
  6. 今天我的公众号改名字啦!
  7. NumPy进阶修炼|你真的了解NumPy吗
  8. 手把手教你使用Matplotlib绘图
  9. 聊聊后端面试中的一些问题和思考
  10. Python办公自动化|从Word到Excel