Android中讓控件在佈局中任意位置擺放的xml配置方法

一、引入:
Android中常用的佈局方式有三種,LinearLayout、RelativeLayout和FrameLayout,相較於後面一種的場景侷限性,前兩者使用的場景更多。在實際開發中,我們經常會遇到下面一種情況,如何將控件在佈局當中的任意位置進行擺放,前面兩種佈局都可以做到,當然,更加推薦使用相對佈局(RelativeLayout),比如類似下面的這種控件擺放場景,直接上效果圖:
在這裏插入圖片描述
爲了區別佈局和控件,我們分別對其進行了背景色的設置,可以看到,所有的控件是垂直分佈的,他們距離邊框或者其它控件的位置不能直接用肉眼測量出來,給人一種任意擺放的感覺,實際上,只要掌握了相對佈局的幾個屬性,確實可以將控件放在佈局中的任意位置。下面先說一些佈局的一些概念,最後講解下這個佈局的代碼實現。

二、線性佈局:
首先需要說明的是,線性佈局也能夠實現上面的效果,但是很麻煩,不過,作爲使用場景最多的佈局,我們還是需要理清一些概念:

  1. layout_gravity和gravity有什麼區別?
    layout_gravity和gravity的區別在於前者針對的是控件在佈局中的位置設置,後者是控件中的內容在控件中的位置設置;
    2. 默認情況下,線性佈局會按照水平方向佈局,所以,需要進行控件的垂直襬放時,請在佈局中設定android:orientation;
  2. 當父佈局的屬性爲android:orientation="horizontal"時,子控件屬性android:layout_gravity="right"將失效(當然還有left等等);
  3. 當父佈局的屬性爲android:orientation="vertical"時,子控件屬性android:layout_gravity="bottom"將失效(當然還有top等等);
    這兩點很重要,歸納起來就是:
    如果父佈局規定了其子控件按照某一方向進行,那麼子控件在該方向上調整自己位置的能力將消失;
    所以,如果我們想要實現一個控件擺放在任意位置,比如與某一邊間隔多少間隙,與某個控件的間隙又是多少等等,如果想要在線性佈局中實現的話,要麼需要使用很多控件進行填白(將控件顏色設置爲背景色),而且,還需要去大量的使用layout_wight這個屬性來進行不同控件的比例分配,才能實現文中開始的那種效果。而相對佈局,就簡單的多了。

三、相對佈局:
我們只需要記住下面的屬性就可以實現控件的任意位置擺放了:
①第一類:屬性值爲true或false

  android:layout_centerHrizontal 水平居中
  android:layout_centerVertical 垂直居中
  android:layout_centerInparent 相對於父元素完全居中
  android:layout_alignParentBottom 貼緊父元素的下邊緣
  android:layout_alignParentLeft 貼緊父元素的左邊緣
  android:layout_alignParentRight 貼緊父元素的右邊緣
  android:layout_alignParentTop 貼緊父元素的上邊緣

②第二類:屬性值必須爲id的引用名“@id/id-name”
  android:layout_below 在某元素的下方
  android:layout_above 在某元素的的上方
  android:layout_toLeftOf 在某元素的左邊
  android:layout_toRightOf 在某元素的右邊
  android:layout_alignTop 本元素的上邊緣和某元素的的上邊緣對齊
  android:layout_alignLeft 本元素的左邊緣和某元素的的左邊緣對齊
  android:layout_alignBottom 本元素的下邊緣和某元素的的下邊緣對齊
  android:layout_alignRight 本元素的右邊緣和某元素的的右邊緣對齊
  
③第三類:屬性值爲具體的值
  android:layout_marginBottom 離某元素底邊緣的距離
  android:layout_marginLeft 離某元素左邊緣的距離
  android:layout_marginRight 離某元素右邊緣的距離
  android:layout_marginTop 離某元素上邊緣的距離
  
可以看到,這些值都是有規律可循的,熟悉了一兩個之後,你就會掌握其他的了,文章開頭的效果圖代碼如下:

<?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:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff0000"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:background="#0000ff"
        android:text="第一個TextView"
        android:textColor="#ffffff"
        android:textSize="25sp" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="15dp"
        android:layout_toRightOf="@+id/textView1"
        android:background="#0000ff"
        android:text="第二個TextView"
        android:textColor="#ffffff"
        android:textSize="25sp" />

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/textView2"
        android:layout_marginTop="60dp"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/textView3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#0000ff"
            android:text="第三個TextView"
            android:textColor="#ffffff"
            android:textSize="25sp" />

        <TextView
            android:id="@+id/textView4"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginLeft="20dp"
            android:background="#0000ff"
            android:text="第四個TextView"
            android:textColor="#ffffff"
            android:textSize="25sp" />
    </LinearLayout>
    
    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/linearLayout1"
        android:layout_marginTop="50dp"
        android:layout_marginLeft="60dp"
        android:background="#0000ff"
        android:text="第五個TextView"
        android:textColor="#ffffff"
        android:textSize="25sp" />


    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView5"
        android:layout_marginTop="70dp"
        android:layout_marginLeft="100dp"
        android:background="#0000ff"
        android:text="第六個TextView"
        android:textColor="#ffffff"
        android:textSize="25sp" />


</RelativeLayout>

代碼中可以看出,相對於父佈局,子佈局和控件都是平級操作的(操作的時候,我們可以直接把子佈局當成一個“大控件”),相對位置的選取,我們一樣可以對佈局進行id的命名來實現,藉助Android studio,我們能夠更加清晰的看到各個控件相對於其他控件的位置:
在這裏插入圖片描述

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