【Android開發學習筆記】【第七課】布局-上

來源:互聯網
上載者:User

標籤:android   style   blog   http   io   ar   color   使用   sp   

概念

Android程式各式各樣,依靠的就是布局,先來看看布局都是怎麼來的:

 

白色部分就是我們經常用的幾種布局,主要說說這個

 

線性布局-LinearLayout

在一個方向上對齊所有元素。

可以橫著、豎著,也可以嵌套,直接看代碼吧

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <!-- vertical 代表垂直布局 -->    <TextView        android:layout_width="fill_parent"        android:layout_height="50dip"        android:background="#88AAFF"        android:gravity="center_horizontal"        android:text="ABCDEFG"        android:textSize="14dip" />    <LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="horizontal" >        <!-- horizontal 代表橫向布局 -->        <TextView            android:layout_width="wrap_content"            android:layout_height="50dip"            android:layout_weight="0"            android:background="#22FFFF"            android:text="hello" />        <TextView            android:layout_width="wrap_content"            android:layout_height="50dip"            android:layout_weight="1"            android:background="#FF22FF"            android:text="world" />        <TextView            android:layout_width="wrap_content"            android:layout_height="50dip"            android:layout_weight="2"            android:background="#2222FF"            android:text="ni" />        <TextView            android:layout_width="wrap_content"            android:layout_height="50dip"            android:layout_weight="3"            android:background="#FFFF22"            android:text="hao" />    </LinearLayout></LinearLayout>

 

根據上面的布局和程式啟動並執行結果,可以得到如下結論:

1、android:orientation="vertical" 代表垂直布局   horizontal 則代表橫向布局

2、android:gravity="center_horizontal" 這一句話使得 ABCDEFG 置中對齊

3、android:layout_weight="0"  這個值決定了占螢幕的百分比的權重

程式運行結果:

 

看起來對於一般的需求,線性布局就夠了。

 

 

相對布局-Relative Layout

聽說是布局裡面功能最強大的,它的存在是為了適應五花八門的螢幕解析度。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent" >    <AnalogClock        android:id="@+id/aclock"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true" />    <!-- layout_centerInParent="true"    置中顯示,將這個控制項顯示在父視窗的中間位置. -->    <DigitalClock        android:id="@+id/dclock"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignLeft="@id/aclock"        android:layout_below="@id/aclock"        android:layout_marginLeft="40px" />    <!--        layout_alignLeft="@id/aclock"       將控制項的左邊緣和給定ID控制項的左邊緣對齊        android:layout_below="@id/aclock"   將控制項置於給定ID控制項之下         layout_marginLeft                   定義的控制項左邊距為40個dip    -->    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignTop="@id/aclock"        android:layout_toLeftOf="@id/dclock"        android:text="目前時間:" />    <!--        android:layout_above="@id/xxx"    將控制項置於給定ID控制項之上         android:layout_toLeftOf           將控制項的右邊緣和給定ID控制項的左邊緣對齊    --></RelativeLayout>

 

根據上面注釋裡面給出來的就是,就可以知道程式運行起來之後是這樣樣子的:

相對布局果然強大,可以隨意布置,下面看一些常用的相對布局使用的屬性的含義:

android:layout_above="@id/xxx"            --將控制項置於給定ID控制項之上
android:layout_below="@id/xxx"            --將控制項置於給定ID控制項之下
android:layout_toLeftOf="@id/xxx"         --將控制項的右邊緣和給定ID控制項的左邊緣對齊
android:layout_toRightOf="@id/xxx"        --將控制項的左邊緣和給定ID控制項的右邊緣對齊
android:layout_alignLeft="@id/xxx"        --將控制項的左邊緣和給定ID控制項的左邊緣對齊
android:layout_alignTop="@id/xxx"         --將控制項的上邊緣和給定ID控制項的上邊緣對齊
android:layout_alignRight="@id/xxx"       --將控制項的右邊緣和給定ID控制項的右邊緣對齊
android:layout_alignBottom="@id/xxx"      --將控制項的底邊緣和給定ID控制項的底邊緣對齊
android:layout_alignParentLeft="true"     --將控制項的左邊緣和父控制項的左邊緣對齊
android:layout_alignParentTop="true"      --將控制項的上邊緣和父控制項的上邊緣對齊
android:layout_alignParentRight="true"    --將控制項的右邊緣和父控制項的右邊緣對齊
android:layout_alignParentBottom="true"   --將控制項的底邊緣和父控制項的底邊緣對齊
android:layout_centerInParent="true"      --將控制項置於父控制項的中心位置
android:layout_centerHorizontal="true"    --將控制項置於水平方向的中心位置
android:layout_centerVertical="true"      --將控制項置於垂直方向的中心位置

【Android開發學習筆記】【第七課】布局-上

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.