1. 程式人生 > >android 佈局2(基本佈局)

android 佈局2(基本佈局)

//線性佈局(LinearLayout)

也就是最開始的佈局

當按鈕超過螢幕(這一行)的時候  就自動會顯示到第二行 

有幾個比較重要的屬性

 android:orientation="horizontal"   顯示在一行

 android:orientation="vertical"   顯示在列(豎著d的行) 

android:layout_weight:權重,用來分配當前控制元件在剩餘空間的大小。
使用權重一般要把分配該權重方向的長度設定為零,比如在水平方向分配權重,就把width設定為零。

比如說  a控制元件裡設定 android:layout_weight=1

            b控制元件裡也設定android:layout_weight=1  

那麼結果會是2/1   各自佔用螢幕的一半 

如果這個佈局檔案裡android:orientation="horizontal" 

那麼結果會是

如果這個佈局檔案裡android:orientation="vertical" 

那麼結果會是橫著切割

這就是兩種不同的排序方式

android:layout_weight 也就是分割螢幕剩餘空間 

如果說  a控制元件裡設定 android:layout_weight=1

            b控制元件裡也設定android:layout_weight=2  

            c控制元件裡也設定android:layout_weight=2  

那麼得到的結果是4/2  4/1 4/1  這樣分割的(主要在於螢幕剩餘空間是什麼)

這一點請轉移到另外一篇部落格看  

連結 :https://blog.csdn.net/yjt520557/article/details/83097431

//絕對佈局  AbsoluteLayout

可以自己手動去調節位置  但是這種方法太麻煩 (程式碼繁多 還需要時間去除錯)

通過        android:layout_x 和         android:layout_y 去改變位置

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="32dp"
        android:layout_y="53dp"
        android:text="Button" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="146dp"
        android:layout_y="53dp"
        android:text="Button" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="85dp"
        android:layout_y="135dp"
        android:text="Button" />

</AbsoluteLayout>

//.幀佈局(FrameLayout)

一幀一幀疊加出來的

佈局子檢視可以疊加

每個子檢視都是一幀

擺放位置只能通過子檢視的Android:layout_gravity方法來設定

幀佈局是Android系統載入速度最快的佈局

就是空間的疊加 

簡單列子:

<FrameLayout xmlns:android="https://schemas.android.com/apk/res/android"

 xmlns:app="https://schemas.android.com/apk/res-auto"

 xmlns:tools="https://schemas.android.com/tools"

 android:layout_width="match_parent"

 android:layout_height="match_parent"

 tools:context=".zhenbj">

<Button

 android:layout_width="300dp"

 android:layout_height="300dp"

 android:background="@android:color/holo_blue_light"

 android:layout_gravity="center"

 />

 <Button

  android:layout_width="200dp"

  android:layout_height="200dp"

  android:background="@android:color/holo_blue_dark"

  android:layout_gravity="center"

  />

 <Button

  android:layout_width="100dp"

  android:layout_height="100dp"

  android:background="@android:color/holo_green_light"

  android:layout_gravity="center"

  />

</FrameLayout>

//網格佈局 一般作用於小格子  不常用 GridLayout

幾個重要的屬性是:

一開始必須知道有幾列 幾行 不然預設顯示在一行裡 

比如:

   android:rowCount="5" //行
    android:columnCount="4"  //列 在開始佈局顯示的

跨行:

 android:layout_rowSpan="2"  //在控制元件中用到的

android:layout_gravity="fill_vertical"//需要填充  

填充可能會多出  所以需要

<Space />(用空白代替) 這是個單獨的控制元件

跨列:

android:layout_columnSpan="3"
android:layout_gravity="fill_horizontal"

RelativeLayout相對佈局

相對佈局是通過相對定位的方式讓控制元件出現在佈局任意位置; 

在相對佈局中如果不指定控制元件擺放的位置,那麼控制元件都會被預設放在RelativeLayout的左上角。因此要先指定第一個控制元件的位置,其他控制元件為該位置的相對位置;

RelativeLayout屬性:

(使用相對佈局屬性需要先指定控制元件的id,其他控制元件根據該控制元件的id,來確定相對於該控制元件的相對位置)

1、基本屬性
    gravity :設定容器內元件的對齊方式
    ignoreGravity : 設定該屬性為true的元件,將不受gravity屬性的影響

2、根據父容器定位
    layout_alignParentLeft : 左對齊
    layout_alignParenRight : 右對齊
    layout_alignParentTop : 頂部對齊
    layout_alignParentButtom : 底部對齊
    android:layout_centerHorizontal :水平居中
    android:layout_centerVertical : 垂直居中
    android:layout_centerInParent : 中間位置

3、根據兄弟元件定位
    layout_toLeftOf : 參考元件的左邊
    layout_toRightOf: 參考元件的左邊
    layout_above : 參考元件的上方
    layout_below : 參考元件的下方
    layout_alignTop :對齊參考元件的上邊界
    layout_alignBottom : 對齊參考元件的下邊界
    layout_alignLeft: 對齊參考元件的左邊界
    layout_alignRight : 對齊參考元件的右邊界

4、margin(偏移)
    設定元件與父容器的邊界
    layout_margin 設定元件上下左右的偏移量
    layout_marginLeft 設定元件離左邊的偏移量
    layout_marginRight 設定元件離右邊的偏移量
    layout_marginTop 設定元件離上面的偏移量
    layout_marginButtom 設定元件離下面的偏移量

5、padding(填充)
    設定元件內部元素間的邊距(比如TextView裡的字型位置)
    android:padding 往內部元素的上下左右填充一定邊距
    paddingLeft 往內部元素的左邊填充一定邊距
    paddingRight 往內部元素的右方填充一定邊距
    paddingTop 往內部元素的上方填充一定邊距
    paddingBottom 往內部元素的下發填充一定邊距

 

(好懶更新的部落格.......加油 明天寫許可權 先建立表關係)