先介紹drawable屬性的區別,這個算是比較簡單的,但是還是有一點點的小細節需要進行說明,drawable有五個檔案夾,分別為hdpi,ldpi,mdpi,xdpi,xxdpi,這五個檔案夾想必大家都知道,其實就是為了適應不同解析度,由於手機解析度的不同,因此我們的圖片需要適應不同手機的解析度...hdpi:480x800 mdpi:480x320 ldpi:320x240xdpi:1280x720 xxdpi 1920x1280其實這個數字並不是非常精確的,只是說明每一個階段都有一個解析度範圍...Android由於和IOS不一樣,IOS是不需要考慮解析度的,但是Android必須要考慮解析度問題,比如說我們在hdpi中放入的圖片在布局中顯示是非常正常的,但是在xdpi裡,那就會變得非常的小...因此我們在設計app的時候,我們需要考慮不同的手機,因此我們需要在這幾個檔案夾中分別放入不同大小的圖片,這樣Andorid系統可以根據手機的解析度來選擇合適的圖片資源進行載入,收一下dip到底表示的是什麼意思,Android中表示大小的一個單位,dpi指像素/英寸,簡單的說一下dpi到底表示的大小值...ldpi指120,mdpi指160,hdpi 指240,xhdpi指320。比如說小米手機是4英寸、854×480的解析度,那麼小米手機的dpi就是854的平方加480的平方和開2次方後除以4,結果大約是245。如果應用安裝在小米手機上,那麼系統會調用圖中drawable-hdpi裡面的資源。這樣,你只要做4套資源分別放在 drawable-ldpi、drawable-mdpi、drawable-hdpi以及drawable-xdpi下(表徵圖可以按照3:4:6:8的 比例製作圖片資源),那麼就可以圖片在不同解析度上有可能失真的情況...
Android中LinearLayout布局技巧
上面只是一個小小的介紹一下,下面來說一下最近發現了一種布局的技巧,原本個人認為RelativeLayout是最好用的布局,同時自己也在使用著表格版面配置,但是發現使用TableLayout已經是很少很少了,沒想到還有一種布局方式是LinearLayout,有人會問,LinearLayout有什麼稀奇的,其實LinearLayout並不稀奇,而是這裡有一個屬性是layout_weight,這樣是我最近才發現的...雖然很早就有了...總之我覺得這個東西是非常好用的,通過使用0dp+layout_weight來進行布局是非常巧妙的一種方式...其實就是實現了比例布局..這樣布局就可以直接適應不同手機的螢幕...從而避免了在不同手機上布局無法同步的問題..舉個例子...
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#0045f5" android:gravity="center" android:text="1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#00ff47" android:gravity="center" android:text="2" android:layout_weight="1"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#ff5600" android:gravity="center" android:layout_weight="1" android:text="3" /></LinearLayout>
這個就是布局後的樣式,我們設定的layout_width=“wrap_content”,按照常理來說,系統應該會為三個TextView分配預設的空間,並且三個空間的大小應該是相同的,但是正是因為我們為後面兩個設定了layout_weight屬性,這樣系統會先為第一個TextView分配預設空間大小,就比如說10dp吧,假設我們的螢幕大小為480x800的解析度,那麼剩下470dp的大小將會按照比例分配給兩個TextView...第二個TextView分配到的大小就是(470/2=225dp),第二個也是225dp...
那麼我們是否就可以做出一個結論呢?就是設定了layout_weight後的控制項,會按指定比例分配剩餘控制項呢?其實並不是這樣的,我們再來看一個布局...
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#0045f5" android:gravity="center" android:text="1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#00ff47" android:gravity="center" android:text="2222222222222222222" android:layout_weight="1"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#ff5600" android:gravity="center" android:layout_weight="1" android:text="3" /></LinearLayout>
這個就是布局的結果,這樣就會出現問題了,其實在設定了wrap_content,系統會先看控制項到底要佔用多少空間,就是先回按照wrap_content對控制項分配空間,由於第二個控制項的預設空間比較大,因此系統只能使用wrap_content對其分配空間,不會再按照layout_weight屬性按照比例分配空間了...因此這裡我們設定layout_width的時候不能設定為wrap_content,我們需要設定成0dp....再看下面的例子....
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <TextView android:layout_width="0dip" android:layout_height="wrap_content" android:background="#0045f5" android:gravity="center" android:layout_weight="1" android:text="1" /> <TextView android:layout_width="0dip" android:layout_height="wrap_content" android:background="#00ff47" android:gravity="center" android:text="2222222222222222222" android:layout_weight="2"/> <TextView android:layout_width="0dip" android:layout_height="wrap_content" android:background="#ff5600" android:gravity="center" android:layout_weight="3" android:text="3" /></LinearLayout>
這裡我們把layout_width=“0dp”,然後配合layout_weight屬性,實現了空間寬度的1:2:3的進行分配,而長度由於我們沒有進行規定,因此使用了wrap_content屬性...這樣0dp配合著layout_weight屬性實現了布局的比例分配...那麼如果我們想要把高度也按照比例分配的話,那麼就把layout_height=“0dp”...然後配合weight屬性就可以同樣實現了布局中高度按照比例進行分配...這裡我們一定要使用0dp...解釋一下Android如何會按照比例布局呢?我們仍然假設我們的螢幕大小是480,那麼由於我們設定的三個TextView大小都為0dp,那麼系統會先按照我們設定的大小進行計算,480-3*0=480,那麼剩餘的空間大小仍然為480dp,最後剩餘的空間按照比例來進行分配...這樣就實現了寬度的1:2:3的大小進行分配...如果我們使用了“fill_parent”屬性,那麼就會出現不相同的效果...在看下面的布局...
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#0045f5" android:gravity="center" android:layout_weight="1" android:text="1" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#00ff47" android:gravity="center" android:text="2" android:layout_weight="2"/> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#ff5600" android:gravity="center" android:layout_weight="2" android:text="3" /></LinearLayout>
這就是我們使用了fill_parent的後果,並沒有按照我們想要的比例出現結果...這裡導致的問題就是,由於我們設定的空間大小都為fill_parent屬性,因此剩餘空間大小就是480-3*480=-960dp,然後按照比例進行分配大小 480+(-960*(1/5))=228dp 480*(-960*(2/5))=96dp 第三個也是96dp...這樣反而導致成了3:1:1的情況出現...這就是使用了fill_parent屬性出現的問題...使用fill_parent這個屬性配合layout_weight屬性,分配的比例是需要我們人為進行計算...看到這裡,是不是已經清晰了呢?
Android:LinearLayout布局中Layout_weight的深刻理解
首先看一下LinearLayout布局中Layout_weight屬性的作用:它是用來分配屬於空間的一個屬性,你可以設定他的權重。很多人不知道剩餘空間是個什麼概念,下面我先來說說剩餘空間。
看下面代碼:
<?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" > <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="left" android:text="one"/> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:layout_weight="1.0" android:text="two"/> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="right" android:text="three"/> </LinearLayout>
運行結果是:
看上面代碼:只有Button2使用了Layout_weight屬性,並賦值為了1,而Button1和Button3沒有設定Layout_weight這個屬性,根據API,可知,他們預設是0
下面我就來講,Layout_weight這個屬性的真正的意思:Android系統先按照你設定的3個Button高度Layout_height值wrap_content,給你分配好他們3個的高度,
然後會把剩下來的螢幕空間全部賦給Button2,因為只有他的權重值是1,這也是為什麼Button2佔了那麼大的一塊空間。
有了以上的理解我們就可以對網上關於Layout_weight這個屬性更讓人費解的效果有一個清晰的認識了。
我們來看這段代碼:
<?xml version="1.0" encoding="UTF-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:background="#ff0000" android:layout_width="**" android:layout_height="wrap_content" android:text="1" android:textColor="@android:color/white" android:layout_weight="1"/> <TextView android:background="#cccccc" android:layout_width="**" android:layout_height="wrap_content" android:text="2" android:textColor="@android:color/black" android:layout_weight="2" /> <TextView android:background="#ddaacc" android:layout_width="**" android:layout_height="wrap_content" android:text="3" android:textColor="@android:color/black" android:layout_weight="3" /> </LinearLayout>
三個文字框的都是 layout_width=“wrap_content ”時,會得到以下效果
按照上面的理解,系統先給3個TextView分配他們的寬度值wrap_content(寬度足以包含他們的內容1,2,3即可),然後會把剩下來的螢幕空間按照1:2:3的比列分配給3個textview,所以就出現了上面的映像。
而當layout_width=“fill_parent”時,如果分別給三個TextView設定他們的Layout_weight為1、2、2的話,就會出現下面的效果:
你會發現1的權重小,反而分的多了,這是為什麼呢???網上很多人說是當layout_width=“fill_parent”時,weighth值越小權重越大,優先順序越高,就好像在背口訣
一樣,其實他們並沒有真正理解這個問題,真正的原因是Layout_width="fill_parent"的原因造成的。依照上面理解我們來分析:
系統先給3個textview分配他們所要的寬度fill_parent,也就是說每一都是填滿他的父控制項,這裡就死螢幕的寬度
那麼這時候的剩餘空間=1個parent_width-3個parent_width=-2個parent_width (parent_width指的是螢幕寬度 )
那麼第一個TextView的實際所佔寬度應該=fill_parent的寬度,即parent_width + 他所佔剩餘空間的權重比列1/5 * 剩餘空間大小(-2 parent_width)=3/5parent_width
同理第二個TextView的實際所佔寬度=parent_width + 2/5*(-2parent_width)=1/5parent_width;
第三個TextView的實際所佔寬度=parent_width + 2/5*(-2parent_width)=1/5parent_width;所以就是3:1:1的比列顯示了。
這樣你也就會明白為什麼當你把三個Layout_weight設定為1、2、3的話,會出現下面的效果了:
第三個直接不顯示了,為什麼呢?一起來按上面方法算一下吧:
系統先給3個textview分配他們所要的寬度fill_parent,也就是說每一都是填滿他的父控制項,這裡就死螢幕的寬度
那麼這時候的剩餘空間=1個parent_width-3個parent_width=-2個parent_width (parent_width指的是螢幕寬度 )
那麼第一個TextView的實際所佔寬度應該=fill_parent的寬度,即parent_width + 他所佔剩餘空間的權重比列1/6 * 剩餘空間大小(-2 parent_width)=2/3parent_width
同理第二個TextView的實際所佔寬度=parent_width + 2/6*(-2parent_width)=1/3parent_width;
第三個TextView的實際所佔寬度=parent_width + 3/6*(-2parent_width)=0parent_width;所以就是2:1:0的比列顯示了。第三個就直接沒有空間了。