android知識點

來源:互聯網
上載者:User

1.用layout_weight實現自適應螢幕

 

一、細說layout_weight    目前最為推薦的Android多螢幕自適應解決方案。    該屬性的作用是決定控制項在其父布局中的顯示權重,一般用於線性布局中。其值越小,則對應的layout_width或layout_height的優先順序就越高,一般橫向布局中,決定的是layout_width的優先順序;縱向布局中,決定的是layout_height的優先順序。    傳統的layout_weight使用方法是將當前控制項的layout_width和layout_height都設定成fill_parent,這樣就可以把控制項的顯示比例完全交給layout_weight;這樣使用的話,就出現了layout_weight越小,顯示比例越大的情況。不過對於2個控制項還好,如果控制項過多,且顯示比例也不相同的時候,控制起來就比較麻煩了,畢竟反比不是那麼好確定的。    於是就有了現在最為流行的 0px設值法。看似讓人難以理解的layout_height=0px的寫法,結合layout_weight,卻可以使控制項成正比例顯示,輕鬆解決了當前Android開發最為頭疼的片段化問題之一。    先看下面的styles(style_layout.xml)

 

?
程式碼片段,雙擊複製
0102030405060708091011121314151617181920212223242526 <?xml version="1.0" encoding="utf-8"?>                                          
<resources>   <!-- 全螢幕展開-->  <style name="layout_full">      <item name="android:layout_width">fill_parent</item>      <item name="android:layout_height">fill_parent</item>    </style>   <!-- 固定自身大小-->  <style name="layout_wrap">      <item name="android:layout_width">wrap_content</item>      <item name="android:layout_height">wrap_content</item>    </style> <!-- 橫向分布-->  <style name="layout_horizontal" parent="layout_full">      <item name="android:layout_width">0px</item>    </style>     <!-- 縱向分布-->  <style name="layout_vertical" parent="layout_full">      <item name="android:layout_height">0px</item>    </style>          </resources>  

 

可以看到,layout_width和layout_height兩個屬性被我封裝成了4個style    根據實際布局情況,選用當中的一種,不需要自己設定,看過我前一個ActivityGroup的Demo的同學應該非常熟悉了    然後我的Demo的布局如下(weight_layout.xml)

 

?
程式碼片段,雙擊複製
010203040506070809101112131415161718192021222324252627282930313233343536373839404142434445464748 <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"        style="@style/layout_full"        android:orientation="vertical">        <LinearLayout                 style="@style/layout_vertical"                android:layout_weight="1"                android:orientation="horizontal">                 <View                         style="@style/layout_horizontal"                         android:background="#aa0000"                         android:layout_weight="1"/>                 <View                         style="@style/layout_horizontal"                         android:background="#00aa00"                         android:layout_weight="4"/>                 <View                         style="@style/layout_horizontal"                         android:background="#0000aa"                         android:layout_weight="3"/>                 <View                         style="@style/layout_horizontal"                         android:background="#aaaaaa"                         android:layout_weight="2"/>                         </LinearLayout>         <LinearLayout                 style="@style/layout_vertical"                android:layout_weight="2"                android:orientation="vertical">                <View                         style="@style/layout_vertical"                         android:background="#ffffff"                         android:layout_weight="4"/>                         <View                         style="@style/layout_vertical"                         android:background="#aa0000"                         android:layout_weight="3"/>                 <View                         style="@style/layout_vertical"                         android:background="#00aa00"                         android:layout_weight="2"/>                 <View                         style="@style/layout_vertical"                         android:background="#0000aa"                         android:layout_weight="1"/>         </LinearLayout></LinearLayout>


整個介面布局看起來非常直觀,只是嵌套的邏輯要自己理下。顯示效果如,其中左面一個是480x800的介面,右面的是320x480的介面(後面的圖也如此),可以看出顯示比例和代碼中完全一致,我就不多說了,大家對照下就能看出來了。

 

2.Timer  and  TimerTask  來定時更新一個程式

  Timer timer = new Timer("");

  timer.schedule(new TimerTask(){},Date date ,long peroid);

  Date表示要在什麼時間運行,peroid表示每隔一段時間之後就會運行一次,這裡如果只想運行一次的話,就不要定義 long peroid 參數了。

 

3.ANR --- 程式不反應

這個會在  data/anr/ trace .txt 中,我們可以分析這個檔案來看一下為什麼會出現這個問題,如所示:

包括了出現的時間,以及在哪個包中出現的,有在 哪個 .java檔案,這樣就很容易定位。看一下是不是 IO 記憶體泄露  堵塞

相關文章

聯繫我們

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