2019-08-20 Android 线性布局介绍-LinearLayout
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布局)
效果图:
设计界面(可视化界面):
AVD上的效果图:
思路:从大到小的布局,均采用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">
strings.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
更多相关文章
- Android相对属性布局总结
- android android:windowSoftInputMode相关属性
- Android布局优化之merge(二)
- Android 属性系统设计分析
- [读书笔记]布局的屏幕适配常用方法
- Android布局优化之ViewStub(三)
- Android属性动画——实现灵动菜单效果
- Android 属性动画ValueAnimator和ObjectAnimator的高级用法