Android:week 2總結 Android佈局一

目錄

1.佈局的通用屬性

2.Linearlayout佈局(線性佈局)

3.RelativeLayout相對佈局


首先需要明白絕對佈局不再使用,多使用相對佈局。

簡單說一下我對Android長度單位的理解:

dp  斜對角(不發生變化)
sp  斜對角(隨着變化而變化) 默認sp
pt  表示一個點
px  像素
in   英尺
mm  毫米

更詳細的解釋

1.佈局的通用屬性

  • android:id
  • android:layout_width
  • android:layout_height
  • android:layout_margin
  • android:padding
  • android:background

android:layout_height和android:layout_width的值可以爲:

     三個屬性都用來適應視圖的水平或垂直大小,一個以視圖的內容或尺寸爲基礎的佈局比精確地指定視圖範圍更加方便。

     1)fill_parent

設置一個構件的佈局爲fill_parent將強制性地使構件擴展,以填充佈局單元內儘可能多的空間。這跟Windows控件的dockstyle屬性大體一致。設置一個頂部佈局或控件爲fill_parent將強制性讓它佈滿整個屏幕。

     2) wrap_content

根據容器內的東西決定組件的大小,比如一個按鈕,按鈕中的字體大,那麼這個按鈕就大,字體小那麼相應的按鈕就會小些。設置一個視圖的尺寸爲wrap_content將強制性地使視圖擴展以顯示全部內容。以TextView和ImageView控件爲例,設置爲wrap_content將完整顯示其內部的文本和圖像。佈局元素將根據內容更改大小。設置一個視圖的尺寸爲wrap_content大體等同於設置Windows控件的Autosize屬性爲True。

     3)match_parent
   Android2.2中match_parent和fill_parent是一個意思 .兩個參數意思一樣,match_parent更貼切,於是從2.2開始兩個詞都可以用。那麼如果考慮低版本的使用情況你就需要用fill_parent了

2.Linearlayout佈局(線性佈局)

     LinearLayout 是一個視圖組,用於使所有子視圖在單個方向(垂直或水平)保持對齊。 您可以使用 android:orientation 屬性指定佈局方向,其值可爲:horizontalvertical分別表示垂直和水平方向。

  1. Orientation (方向)
  2. layout_weight (權重)
<?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:tool="http://schemas.android.com/tools"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    tool:context=".MainActivity"
    android:orientation="horizontal"
    android:layout_centerVertical="true"
    android:layout_alignParentBottom="true">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="確定"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:text="取消"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:text="複製"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="粘貼"/>
</LinearLayout>

 

佈局權重

LinearLayout 還支持使用 android:layout_weight 屬性爲各個子視圖分配權重。子視圖可以指定權重值,然後系統會按照子視圖聲明的權重值的比例,將視圖組中的任何剩餘空間按權重比例分配給子視圖。 默認權重爲零。

    例如,如果有三個文本字段,其中兩個聲明權重爲 1,另一個沒有賦予權重,則沒有權重的第三個文本字段將不會擴展到剩餘空間,並且僅佔據其內容所需的區域,大小由layout_width指定。 所有三個字段定義長度後還剩餘的空間,將被另外兩個文本字段將以同等幅度進行均分。 如果爲第三個字段提供權重 2(而不再是 0),那麼相當於聲明現在它比其他兩個字段更爲重要,因此,它將獲得總剩餘空間的一半,其他兩個均享餘下空間。

權重相等的子視圖

要創建一個線性佈局,讓每個子視圖在屏幕上都佔據相同的空間量,對於垂直佈局,則將每個視圖的 android:layout_height 均設置爲 "0dp";對於水平佈局,將每個視圖的android:layout_width 均設置爲 "0dp"。 然後,將每個視圖的 android:layout_weight 均設置爲 "1"。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tool="http://schemas.android.com/tools"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    tool:context=".MainActivity"
    android:orientation="horizontal"
    android:layout_centerVertical="true"
    android:layout_alignParentBottom="true">

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="確定"/>
    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="取消"/>
    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="複製"/>
    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="粘貼"/>
</LinearLayout>

和上述代碼區分權重值和layout_width值。

<Button>元素中的android:layout_gravity,表示<Button>在包含它的父ViewGroup <LinearLayout>中的對齊方式。

 

 

3.RelativeLayout相對佈局

RelativeLayout以相對位置顯示各子view的位置,每個view的位置通過相對它的兄弟view或者它的父view來進行定位。

定義View相對位置的屬性爲android:layout_something,所有這類屬性如下表:

屬性名 描述
android:layout_above 在指定控件的上邊
android:layout_alignBaseline 同指定控件在同一基線上
android:layout_alignBottom 與指定控件底部對齊
android:layout_alignEnd 與指定控件的end對齊
android:layout_alignLeft 與指定控件的左邊對齊
android:layout_alignParentBottom 值爲true, 與父佈局底部對齊
android:layout_alignParentEnd 值爲true,與父佈局的end對齊
android:layout_alignParentLeft 值爲true, 與父佈局的左邊對齊
android:layout_alignParentRight 值爲true,與父佈局的右邊對齊
android:layout_alignParentStart 值爲true, 與父佈局的start對齊
android:layout_alignParentTop 值爲true, 與父佈局的頂部對齊
android:layout_alignRight 同指定控件的右邊對齊
android:layout_alignStart 同指定控件的start對齊
android:layout_alignTop 同指定控件的頂部對齊
android:layout_alignWithParentIfMissing 設置爲true時,如果layout_toLeftOf, layout_toRightOf, etc的對齊對象缺失的時候,則改爲父佈局。
android:layout_below 在指定控件的下面
android:layout_centerHorizontal 值爲 true, 在父佈局的水平中間對齊
android:layout_centerInParent 值爲 true, 在父佈局的水平垂直對齊
android:layout_centerVertical 值爲 true, 在父佈局的垂直對齊
android:layout_toEndOf 在指定控件的末端
android:layout_toLeftOf 在指定控件的左邊
android:layout_toRightOf 在指定控件的右邊
android:layout_toStartOf 在指定空間的始端

 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tool="http://schemas.android.com/tools"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    tool:context=".MainActivity"  >
<!--    android:layout_gravity="center_vertical"   對佈局整體起作用-->

<!--相對佈局 orientation不起作用,只在線性佈局起作用-->
    <Button
        android:id="@+id/bt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="確定"/>
    <Button
        android:id="@+id/bt2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/bt1"
        android:layout_below="@+id/bt1"
        android:text="取消"/>
    <Button
        android:id="@+id/bt3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/bt2"
        android:layout_below="@+id/bt2"
        android:text="複製"/>
    <Button
        android:id="@+id/bt4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/bt3"
        android:layout_below="@+id/bt3"
        android:text="粘貼"/>
    <!--        只對button控件起作用-->
</RelativeLayout>

記得看註釋!!!

 

背景圖片或背景顏色:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tool="http://schemas.android.com/tools"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    tool:context=".MainActivity"  >
<!--    android:layout_gravity="center_vertical"   對佈局整體起作用-->

<!--相對佈局 orientation不起作用,只在線性佈局起作用-->
    <Button
        android:id="@+id/bt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:background="#80FFC0CB"
        android:text="bt111"/>
    <Button
        android:id="@+id/bt2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/bt1"
        android:layout_below="@+id/bt1"
        android:background="#ffFFC0CB"
        android:text="bt222"/>

</RelativeLayout>

圖片:android:background="@mippmap/name"

顏色:android:background="@mippmap/#ffffff"

顏色和不透明度 (alpha) 值以十六進制表示法表示。任何一種顏色的值範圍都是 0 到 255(00 到 ff)。對於 alpha,00 表示完全透明,ff 表示完全不透明。android:background的值的格式爲”#AARRGGBB”。AA即透明度,R、G、B是紅綠藍三色。每一位均爲0–F的十六位數。其中透明度的數值越大,越不透明。

其他顏色:https://www.cnblogs.com/android100/p/android-setalpha.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章