Loading

Android学习笔记-LinearLayout-线性布局

Android中有六大布局,分别是: LinearLayout(线性布局),RelativeLayout(相对布局),TableLayout(表格布局) FrameLayout(帧布局),AbsoluteLayout(绝对布局),GridLayout(网格布局) 

用到最多的是前两种:LinearLayout(线性布局),RelativeLayout(相对布局)

先试试线性布局:

    <LinearLayout
android:layout_width="200dp"
设置区域宽度    200dp表示200个单位的宽度 dp是这里常用的单位
android:layout_height="match_parent"
设置区域高度 match_parent   代表适应屏幕的高度(或者宽度)
        android:orientation="vertical"
代表在垂直方向上布局
android:background="#66CCFF"
设置区域颜色
android:paddingLeft="40dp"
android:paddingRight="20dp"
android:paddingTop="10dp"
android:paddingBottom="30dp"
上面这四个分别代表区域内的子区域至少要距离此区域边界的长度
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
这两个代表区域至少距离屏幕的距离 可以看到至少要距离15dp
>

<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ccff00"
/>
<View/>内部的代码是其子区域的属性,match_parent即为匹配边界最大值,那么子区域的大小则由上面四个padding决定
    </LinearLayout>
这段代码预览如图,可以看到高度是适应了屏幕的高度,宽度是200,约一半,内部子区域距离边界分别是40dp20dp10dp30dp,
并且左侧距离边界至少为15dp
我们把代码扩充一下,最后变成这样
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="200dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#66CCFF"
android:paddingLeft="40dp"
android:paddingRight="20dp"
android:paddingTop="10dp"
android:paddingBottom="30dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
>
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ccff00"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#66CC33"
android:orientation="vertical"
android:paddingLeft="40dp"
android:paddingTop="10dp"
android:paddingRight="20dp"
android:paddingBottom="30dp">
<View
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#ffff00" />
</LinearLayout>
</LinearLayout>

 

 然后就变成这样的了,是同样的道理

我们再看下一个例子

<LinearLayout
android:layout_width="350dp"
android:layout_height="200dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
android:layout_marginRight="15dp"
android:layout_marginBottom="15dp"
android:background="#66CCFF"
android:orientation="horizontal"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:paddingRight="10dp"
android:paddingBottom="10dp">

<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ccff00" />

<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ff0033" />
</LinearLayout>
这里的关键点在android:layout_weight="1",这个weight是权重的意思,它的意思是,
除开本身数据的dp之后剩下的区域会根据两个区域的权重比值分配(可以看到两个区域的本来的宽度其实是零),可以看到我这里是变成了平分,
因为他们的权重都是1,是相等的,如果把第二个改成2的权重,就变成下一张图的样子了

 

 

 



posted @ 2023-02-23 19:59  冰稀饭Aurora  阅读(39)  评论(0编辑  收藏  举报