Adnroid零基礎之線性佈局與相對佈局

. Linearlayout

線性佈局中,有4個及其重要的參數,直接決定元素的佈局和位置,這四個參數是:

  1. android:layout_gravity (是本元素相對於父元素的重力方向
  2. android:gravity (是本元素所有子元素的重力方向)
  3. android:orientation (線性佈局以列或行來顯示內部子元素)
  4. android:layout_weight (線性佈局內子元素對未佔用空間【水或垂直】分配權重值,其值越小,權重越大。前提是子元素
    設置了水平方向的屬性或垂直方向的屬性如果某個子元素的

下面舉個例子

首先打開Android Stdio。新建項目

這裏寫圖片描述

然後在res文件夾下Layout中的佈局文件編寫代碼

這裏寫圖片描述

這裏寫圖片描述
新建一個按鈕,可以拖動,也可以編碼。

<Button
        android:layout_width="match_pat"
        android:layout_height="wrap_content"
        android:text="1"/>

LinearLayout屬性中Android:orientation爲設置線性佈局當其="vertical"時,爲 垂直線性佈局,當其="horizontal"時,爲水平線性佈局,不管是水平還是垂直線性佈局一行(列)只能放置一個控件。

LinearLayout也支持用android:layout_weight屬性爲單個子view指定權重(weight)。這個屬性爲一個view指定一個非常重要的值,此值指定了該view需要佔用屏幕上多大的空間。一個更大的權重值運行子view擴展到填充滿其父view的剩餘空間。子view能夠指定權重值,然後view組中的剩餘空間會按照聲明的權重所佔的比例來分配。默認的權重是 0

<?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">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1"
        android:layout_weight="1"
        android:layout_gravity="top"
        android:gravity="left"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2"
        android:gravity="right"
        android:layout_weight="3"
        android:layout_gravity="center_vertical"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="3"
        android:layout_weight="2"
        android:layout_gravity="bottom"
        android:gravity="bottom"/>


</LinearLayout>

這裏寫圖片描述

二.RativeLayout

RelativeLayout是一種相對佈局,控件的位置是按照相對位置來計算的,後一個控件在什麼位置依賴於前一個控件的基本位置,是佈局最常用的。相對佈局的屬性有很多比如:

android:layout_marginTop=“25dip” //頂部距離
android:gravity=“left” //空間佈局位置
android:layout_marginLeft="15dip //距離左邊距

// 相對於給定ID控件
android:layout_above 將該控件的底部置於給定ID的控件之上;
android:layout_below 將該控件的底部置於給定ID的控件之下;
android:layout_toLeftOf 將該控件的右邊緣與給定ID的控件左邊緣對齊;
android:layout_toRightOf 將該控件的左邊緣與給定ID的控件右邊緣對齊;

android:layout_alignBaseline 將該控件的baseline與給定ID的baseline對齊;
android:layout_alignTop 將該控件的頂部邊緣與給定ID的頂部邊緣對齊;
android:layout_alignBottom 將該控件的底部邊緣與給定ID的底部邊緣對齊;
android:layout_alignLeft 將該控件的左邊緣與給定ID的左邊緣對齊;
android:layout_alignRight 將該控件的右邊緣與給定ID的右邊緣對齊;

// 相對於父組件
android:layout_alignParentTop 如果爲true,將該控件的頂部與其父控件的頂部對齊;
android:layout_alignParentBottom 如果爲true,將該控件的底部與其父控件的底部對齊;
android:layout_alignParentLeft 如果爲true,將該控件的左部與其父控件的左部對齊;
android:layout_alignParentRight 如果爲true,將該控件的右部與其父控件的右部對齊;

// 居中
android:layout_centerHorizontal 如果爲true,將該控件的置於水平居中;
android:layout_centerVertical 如果爲true,將該控件的置於垂直居中;
android:layout_centerInParent 如果爲true,將該控件的置於父控件的中央;

// 指定移動像素
android:layout_marginTop 上偏移的值;
android:layout_marginBottom 下偏移的值;
android:layout_marginLeft   左偏移的值;
android:layout_marginRight   右偏移的值;
Android;layout_above/below對於相對控件的上方。下方。

下面舉個小例子

這裏寫圖片描述

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

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="1" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="1" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"

        android:text="1" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentBottom="true"
        android:text="1" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_nParentBottom="true"
        android:text="1" /> 
 
      
</RativeLayout>

表格佈局

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