2013年7月31日 星期三

[Android] RelativeLayout範例介紹

在Android裡,RelativeLayout是一個最彈性的的Layout,
主要是利用上、下、左、右(above, below, left and right)來排列component的位置,
以我們這個例子來說,button2在button1的下面,button3在button2的右下方,button4在button3的右下方

File : res/layout/main.xml
<RelativeLayout 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" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Button1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:text="Button2" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button2"
        android:layout_toRightOf="@+id/button2"
        android:text="Button3" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button3"
        android:layout_toRightOf="@+id/button3"
        android:text="Button4" />

</RelativeLayout>
結果:

[Android] TableLayout 範例介紹

在這個例子,我們來看如何用TableLayout來排列textView和button,
及如何使用“android:layout_span”將button寬度延伸成2欄位(column),
及使用“android:layout_column”來將按鈕放到特定的欄位。

File : res/layout/main.xml 
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tableLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
 
    <!-- 2個欄位 -->
    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dip" >
 
        <TextView
            android:id="@+id/textView1"
            android:text="第 1 欄"
            android:textAppearance="?android:attr/textAppearanceLarge" />
 
        <Button
            android:id="@+id/button1"
            android:text="第 2 欄" />
    </TableRow>
 
    <!-- 將按鈕延伸至2個欄位寬  -->
    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dip" >
 
         <Button
            android:id="@+id/button2"
            android:layout_span="2"
            android:text="第 1 和 2 欄" />
         
       
    </TableRow>
 
 
    <!-- 3個欄位 -->
    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dip" >
 
        <TextView
            android:id="@+id/textView2"
            android:text="第 1 欄"
            android:textAppearance="?android:attr/textAppearanceLarge" />
 
        <Button
            android:id="@+id/button2"
            android:text="第 2 欄" />
 
        <Button
            android:id="@+id/button3"
            android:text="第 3 欄" />
    </TableRow>
 
    <!-- 利用layout_columnundefinedbase is 0),將按鈕放在第2欄 -->
    <TableRow
        android:id="@+id/tableRow4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dip" >
 
        <Button
            android:id="@+id/button4"
            android:layout_column="1"
            android:text="第 2 欄" />
    </TableRow>
 
    <!-- 利用layout_columnundefinedbase is 0),將按鈕放在第3欄 -->
    <TableRow
        android:id="@+id/tableRow5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dip" >
 
        <Button
            android:id="@+id/button5"
            android:layout_column="2"
            android:text="第  3 欄" />
    </TableRow>
 
</TableLayout>

結果:



















延伸閱讀: [Android] RelativeLayout範例介紹

2013年7月21日 星期日

[Android] LinearLayout 範例介紹

LinearLayout是一個很常用的Layout,
主用是用orientation屬性來把component(如button)做垂直及水平的排列,
另外,帶有權重(weight)的component將會填滿剩餘的空間

我們來舉個例子,來看看如何把component做垂直及水平的排列,及weight如何使用

1. LinearLayout – 水平(Horizontal)
打開res/layout/main.xml,加入 android:orientation="horizontal",然後增加3個按鈕,
在這個例子中,我們將權重設在button3,所以它將會填滿剩餘的空間

res/layout/main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >
 
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button1" />
 
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button2" />
 
    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button3" 
        android:layout_weight="1"/>
 
</LinearLayout>
結果:



2. LinearLayout – 垂直(Vertical)
我們把orientation改成“Vertical

res/layout/main.xml
<?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="fill_parent"
    android:orientation="vertical" >
 
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button1" />
 
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button2" />
 
    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button3" 
        android:layout_weight="1"/>
 
</LinearLayout>
結果:

[Android] 解決問題Your project contains errors,please fix them before running your application.

今天開啟eclipse要使用時,發現無法編譯,只出現Your project contains errors,please fix them before running your application.









 檢查所有程式,也找不到任何錯誤,只有project name上出現一個紅色叉叉,
 一般這個問題,可能是無法讀取R.class,我會先刪除gen\R.java檔,然後refresh
 然後Project>clean整個project,最後重新編譯一次。
 但這次這個方法沒用,哈























最後google發現是Debug認證已經過期,解決方法很簡單,
進入windows>Preferences>Android->Build,找到debug.keystore的路徑,刪除就可以了,
系統會在自己產生一個

2013年7月7日 星期日

[Android] adb logcat使用方法

● Logcat是一個即時觀看系統訊息(system messages)的工具,一般會利用ADB(android debug bridge)來啟動,如果是用eclipse開發,就在windows\ShowView裡,選LogCat,就可以打開了。

這裡我們利用ADB來作示範,在裝完android SDK後,裡面就會包含了ADB,
以Windows為例,adb路徑會在android-sdks\platform-tools\adb.exe,所以在開啟terminal後,要先切到這個路徑下。
而Linux,裝好SDK,開啟terminal就可以用了。

使用之前裝置端要先開啟USB偵錯模式,路徑如下,設定>開發人員選項>USB偵錯
然後在電腦端輸入
$ adb devices
會得到下面結果,表示已和裝置端連接成功



● 下列是Log class提供了幾個列印message到logcat的方法
v(String, String) (verbose)
d(String, String) (debug)
i(String, String) (information)
w(String, String) (warning)
e(String, String) (error)

一般在開發程式時,會在每個function的的第一行加上log,方便日後trace code
像下面這樣
Log.i("MyActivity", "test"); //log.i裡的i是指(information)
LogCat就會輸出log成這樣
I/MyActivity( 1555): test


● 如何輸出LOG呢
$ adb logcat
但這種方法log太多了,會印出全部的log,包含verbose/debug/information/warning/error
所以我們需要用到下列的方法過濾出我們要的部分


● 過濾log(Filtering Log Output)
每一個Android的log都包含了標籤(tag),和優先權(priority)
例如,I/MyActivity( 1557): test //I是優先權,又分成了下列幾種;MyActivity就是標籤
Android優先權:
V — Verbose (最低優先權)
D — Debug
I — Info
W — Warning
E — Error
F — Fatal
S — Silent (最高優先權,S表示不會用印出任何LOG)
為什麼分成七級呢?看到例子三就明白了

知道了android log之後,要如何過濾呢?我們用下面幾個例子來學習學習
例子一
//如果只想印出這種寫法Log.i("MyActivity", "test")的log,則要這樣寫

$ adb logcat MyActivity:I *:S

//MyActivity:I是表示,印出MyActivity標籤及information優先權的log

//*:S 是指其它所有的log都silent,也就是都不印出來

例子二
//如果我們一次想看這2種logs,則該怎麼寫呢?

//Log.i("MyActivity", "test");

//Log.d("MyActivity2", "test2");

$ adb logcat MyActivity:I MyActivity2:D *:S

例子三
那如果這樣寫$ adb logcat *:W 會發生什麼事呢?

這樣就會印出所有log,且優先權在Warning以上的log,也就是Warning/Error/Fatal



● 延伸
一般我們在做phone的project時,就會常用到
$ adb logcat -v time -b radio
//-v time是指出時間,-b radio是指印出radio和telephony相關訊息


●更多參考資料:
http://developer.android.com/tools/debugging/debugging-log.html#startingLogcat