像海浪撞過了山丘以后還能撐多久
他可能只為你贊美一句后往回流
那嬌艷的花盛開后等你來能撐多久
還是被詩人折斷了傷心了
換歌詞一首
那鴛鴦走散了一只在拼命的往南走
被混沌的城市用鋼筋捂住了出口
仿佛悲傷的人們
能靠著霧霾遮住傷口
還羨慕著期待藍(lán)天的少年總抬頭
.....
前言
通過 L1-1B 的課程學(xué)習(xí)潘拱,我 get 到了 ViewGroup 和兩種基本布局 LinearLayout(線性布局)哈雏、RelativeLayout(相對(duì)布局) 的知識(shí),那么現(xiàn)在我們來一一了解這些東西吧廷臼。
1. ViewGroup
- 什么是 ViewGroup ? 運(yùn)用官放文檔的原話
A ViewGroup is a special view that can contain other views (called children.) The view group is the base class for layouts and views containers. This class also defines the ViewGroup.LayoutParams
class which serves as the base class for layouts parameters.
簡單的翻譯一些就是:ViewGroup 是一個(gè)可以包含其他視圖(稱為子對(duì)象)的特殊視圖。視圖組是布局和視圖容器的基類。 這個(gè)類還定義了 ViewGroup.LayoutParams 類,用作布局參數(shù)的基類骤铃。(渣渣英語勿噴)
根據(jù)字面上的解釋應(yīng)該很好了解了,如果還想深入了解請(qǐng)移步到 官網(wǎng)溃列。
2. LinearLayout(線性布局)
- 什么是 LinearLayout(線性布局),繼續(xù)放出官方原話(點(diǎn)擊跳轉(zhuǎn)到官網(wǎng))
A Layout that arranges its children in a single column or a single row. The direction of the row can be set by calling setOrientation(). You can also specify gravity, which specifies the alignment of all the child elements by calling setGravity() or specify that specific children grow to fill up any remaining space in the layout by setting the weight member of LinearLayout.LayoutParams. The default orientation is horizontal.
渣渣英語又來翻譯了:將其子元素排列在單列或單行中的布局劲厌。 可以通過調(diào)用 setOrientation() 設(shè)置行的方向。 您還可以通過調(diào)用 setGravity() 來指定所有子元素的對(duì)齊方式听隐,或指定特定子項(xiàng)通過設(shè)置 LinearLayout.LayoutParams 的 weight 成員來填充布局中的任何剩余空間补鼻。 默認(rèn)方向?yàn)?水平。
- 現(xiàn)在我們來看 LinearLayout 的簡單用法
<?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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是小一"
android:textColor="@color/colorPrimary"
android:textSize="24sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是小二"
android:textColor="#1d953f"
android:textSize="24sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是小三"
android:textColor="#f15b6c"
android:textSize="24sp"/>
</LinearLayout>
我在 **LinearLayout **中添加了 3 個(gè) TextView ,每個(gè) TextView 的長和寬都是 wrap_content ,并指定排序方式為 vertical 雅任,然后它就會(huì)如下圖顯示一樣:
現(xiàn)在我們來看 LinearLayout 的屬性
** android:orientation="vertical" ** :LinearLayout 的排序方式风范,有 vertical(垂直方向)和 horizontal(水平方向)
現(xiàn)在我們將 android:orientation 的屬性改為 horizontal
<?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="horizontal">
...
</LinearLayout>
修改代碼后 LinearLayout 中的控件會(huì)在水平方向上依次排列,如下圖
根據(jù)官方原話說:可以通過調(diào)用 setGravity() 來指定所有子元素的對(duì)齊方式 沪么,那么現(xiàn)在我們?cè)趤砜?android:layout_gravity 屬性
二話不說硼婿,先上代碼:
<?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="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是小一"
android:textColor="@color/colorPrimary"
android:textSize="24sp"
android:layout_gravity="top"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是小二"
android:textColor="#1d953f"
android:textSize="24sp"
android:layout_gravity="center_vertical"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是小三"
android:textColor="#f15b6c"
android:textSize="24sp"
android:layout_gravity="bottom"/>
</LinearLayout>
我將第一個(gè) TextView 的對(duì)其方式指定為 top,第二個(gè) TextView 的對(duì)其方式指定為 center_vertical禽车,第三個(gè) TextView 的對(duì)其方式指定為 bottom寇漫,因?yàn)楫?dāng)前 LinearLayout 的排列方式為 horizontal,所以當(dāng)前的效果如下圖顯示:
在學(xué)習(xí) LinearLayout 過程中我還學(xué)到了一個(gè)重要的屬性
android:layout_weight (權(quán)重) 殉摔,現(xiàn)在我們來看它的用法:
<?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="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@color/colorPrimary"
android:text="我是小一"
android:textSize="24sp"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#1d953f"
android:text="我是小二"
android:textSize="24sp"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#f15b6c"
android:text="我是小三"
android:textSize="24sp"/>
</LinearLayout>
根據(jù)代碼顯示州胳,當(dāng)前 LinearLayout 的排列方式為 horizontal,我把 3 個(gè) TextView 的 android:layout_width 屬性設(shè)為 ''0dp''逸月,然后在每個(gè) TextView 中加入了 android:layout_weight="1" 屬性栓撞,然后它的效果就會(huì)如下圖顯示:
根據(jù)上圖分析可以得到玛迄,我加入了 android:layout_weight="1" 屬性之后欧引,3 個(gè) TextVeiw 就平分了整個(gè)屏幕,每個(gè) TextVeiw 各占一份蝎毡,從而得到 權(quán)重 是與 屏幕比例相關(guān)的
注意: 當(dāng)我們?cè)谒椒较蛏鲜褂?weight 屬性的時(shí)候笛钝,控件所占屏幕比和控件的高度本身沒有任何關(guān)系壶熏,只是和高度的值有關(guān)系容客。反之一樣
- 到現(xiàn)在為止關(guān)于 LinearLayou 的屬性就介紹到這里了艾恼,如果想深入了解,請(qǐng)查閱官方文檔剃浇。
3. RelativeLayout(相對(duì)布局)
關(guān)于 RelativeLayout 我就不想過多的做解釋了巾兆,在這里主要放出官方文檔的原意和基本屬性。
A Layout where the positions of the children can be described in relation to each other or to the parent. 點(diǎn)擊跳轉(zhuǎn)到官方
上面的意思就是:可以相對(duì)于彼此或相對(duì)于父母描述子元素的位置的布局虎囚。
- 基本屬性:
android:layout_alignParentTop="true":將部件的頂部與容器的頂部對(duì)齊
android:layout_alignParentBottom="true":將部件的底部與容器的底部對(duì)齊
android:layout_alignParentStart="true":將部件的左側(cè)與容器的左側(cè)對(duì)齊
android:layout_alignParentEnd="true":將部件的右側(cè)與容器的右側(cè)對(duì)齊
android:layout_centerInParent="true"":部件在容器中水平垂直居中
android:layout_centerHorizontal="true"":部件在容器中垂直居中
android:layout_centerVertical="true"":部件在容器中水平居中
android:layout_below="@+id/控件名字":該控件在某控件的下方
android:layout_above="@+id/控件名字":該控件在某控件的上方
android:layout_toStartOf="@+id/控件名字":該控件在某控件的左方
android:layout_toEndOf="@+id/控件名字":該控件在某控件的右方
4. 總結(jié)
好了,就到這里了蔫磨,其實(shí)關(guān)于布局不止有 LinearLayout(線性布局)淘讥、RelativeLayout(相對(duì)布局),在 Android 中還有 FrameLayout(幀布局)堤如、AbsoluteLayout(絕對(duì)布局)蒲列,TableLayout(表格布局)、GridLayout(網(wǎng)格布局)搀罢、PercentLayout(百分比布局)蝗岖、ConstraintLayout(約束布局)等等。以上就是我通過 L1-1B 的課程學(xué)習(xí)到的知識(shí)點(diǎn)榔至,雖然很簡單抵赢,但做任何是都是由簡單到復(fù)雜的!
混子性打野唧取,等我混起來之時(shí)铅鲤,就是我 carry 之日