Android學習筆記1

來源:互聯網
上載者:User

AbsoluteLayout ,是一個按照絕對座標定義的布局,由於使用絕對座標去定位控制項,因此要實現自適應介面時,應盡少使用 AbsoluteLayout 。
 AbsoluteLayout 裡面的控制項都以layout_x 、layout_y 來定義其位置:

<AbsoluteLayout
android:id="@+id/AbsoluteLayout01" android:layout_height="wrap_content"
android:layout_width="fill_parent" > <TextView
android:text="TextView01" android:id="@+id/TextView01"
android:layout_height="wrap_content" android:layout_y="10px"
android:layout_width="wrap_content" android:layout_x="110px">
</TextView> </AbsoluteLayout>

LinearLayout布局

LinearLayout布局將自己的子項目按照一個方向排列,即水平排列或豎直排列。

 

幾個xml屬性:
1,android:orientation

布局方向。horizontal是讓所有的子項目按水平方向從左至右排列, vertical是讓所有的子項目按豎直方向從上到下排列。

2,android:gravity 與 android:layout_gravity的區別

android:gravity是指定本元素的子項目相對它的對齊。

android:layout_gravity是指定本元素相對它的父元素的對齊。

例如:

下面這裡的linearlayout的android:gravity設為right,有兩個子項目Button01和Button02。

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:gravity=”right”
>
<Button android:text=”button01″ android:id=”@+id/Button01″ android:layout_width=”wrap_content” android:layout_height=”wrap_content”></Button>
<Button android:text=”button02″ android:id=”@+id/Button02″ android:layout_width=”wrap_content” android:layout_height=”wrap_content”></Button>
</LinearLayout>
下面是,可以看到都居右了。

 

這個main.xml裡的LinearLayout也是有兩個子項目Button01和Button02。Button01的android:layout_gravity設為”left”,Button02的 android:layout_gravity設為”right”

<?xml version=”1.0″ encoding=”utf-8″?>

<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”

android:orientation=”vertical”

android:layout_width=”fill_parent”

android:layout_height=”fill_parent”

>

<Button android:layout_gravity=”left” android:text=”button01″ android:id=”@+id/Button01″ android:layout_width=”wrap_content” android:layout_height=”wrap_content”></Button>

<Button android:layout_gravity=”right” android:text=”button02″ android:id=”@+id/Button02″ android:layout_width=”wrap_content” android:layout_height=”wrap_content”></Button>

</LinearLayout>

 

px (pixels)像素
dip (device independent pixels)裝置獨立像素
sp (scaled pixels — best for text size)放大像素--對文字大小最好
pt (points)點
in (inches)英寸
mm (millimeters)毫米

長度單位dp、sp和px的區別

看到有很多網友不太理解dp、sp和px的區別:現在這裡介紹一下,dp也就是dip。這個和sp基本類似。
如果設定表示長度、高度等屬性時可以使用dp或sp。但如果設定字型,需要使用sp。dp是與密度無關,sp除了與密度無關外,還與scale無關
。如果螢幕密度為160,這時dp和sp和px是一樣的。1dp=1sp=1px,但如果使用px作單位,如果螢幕大小不變(假設還是3.2寸),
而螢幕密度變成了320。那麼原來TextView的寬度設成160px,在密度為320的3.2寸螢幕裡看要比在密度為160的3.2寸螢幕上看短了一半。
但如果設定成160dp或160sp的話。系統會自動將width屬性值設定成320px的。也就是160 * 320 / 160。其中320 / 160可稱為密度比例因素。
也就是說,如果使用dp和sp,系統會根據螢幕密度的變化自動進行轉換.

 

drawable- hdpi、drawable- mdpi、drawable-ldpi的區別:

  (1)drawable-hdpi裡面存放高解析度的圖片,如WVGA (480x800),FWVGA (480x854)

  (2)drawable-mdpi裡面存放中等解析度的圖片,如HVGA (320x480)

  (3)drawable-ldpi裡面存放低解析度的圖片,如QVGA (240x320)

  系統會根據機器的解析度來分別到這幾個檔案夾裡面去找對應的圖片。

  在開發程式時為了相容不同平台不同螢幕,建議各自檔案夾根據需求均存放不同版本圖片。

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.