2015年5月25日 星期一

ListView的item中設定元件layout_weight

目的:

在listview的item中設定元件layout_weight

操作:

  1. 在item的xml中,每個元件設定layout_weight
  2. listview的設定,layout_width="match_parent"

Code:

main.xml

    <ListView
        android:id="@+id/totalReview_detailLV"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>




item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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:layout_weight="2" >
    </TextView>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" >
    </TextView>

</LinearLayout>

reference:
http://stackoverflow.com/a/28078042

2015年5月18日 星期一

SQLite using rawQuery

目的:

在android中使用SQLiteDatabase.rawQuery( query, whereArgs ) 進行query。

操作:

            String query = "SELECT r.id FROM %s c INNER JOIN %s r ON c.name = r.name WHERE c.id = ?" ;
            query = String.format( query, src_table_name, change_table_name, 0 ) ;
            String[] whereArgs = new String[] { String.valueOf( id ) } ;
            Cursor c = db.rawQuery( query, whereArgs ) ;

Q1. 「?」使用在table name位置時,android 會出現SQLException,無法執行。

A1. 使用String.format,搭配「%s」方式導入table name,但其他條件仍使用whereArgs方式,在query時帶入。

2015年5月17日 星期日

SQLite 時間

目的:

在SQLite中insert時,default值填入current time。

操作:

CREATE TABLE IF NOT EXISTS `test` (
  `curr_time` TIMESTAMP DEFAULT (datetime('now','localtime')) -- 時間
)

  1. `curr_time` TIMESTAMP DEFAULT (datetime('now','localtime')) :目前手機時間
  2. `curr_time` TIMESTAMP DEFAULT (datetime(CURRENT_TIMESTAMP,'localtime')) :目前手機時間
  3. `curr_time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP :時間有可能非手機目前的時間

insert時間:

in Android:

            ContentValues contentValues = new ContentValues() ;
            contentValues.put( "curr_time", new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ).format( new Date() ) ) ;
            success = ( db.update( "test", contentValues, null, null ) > 0 ) ;

2015年5月10日 星期日

DrawerLayout搭配ActionBar,設定HomeAsUp

目的:

第一頁使用側拉抽屜,HomeAsUp根據抽屜是否開啟而自動變換icon。
第二頁使用側拉抽屜,HomeAsUp直接固定使用返回icon,抽屜拉開不會改變icon。

元件:

  1. android.support.v4.widget.DrawerLayout
  2. android.support.v7.widget.ActionBarDrawerToggle

操作:

  1. 設定DrawerLayout 和 ActionBarDrawerToggle
  2. 設定啟用HomeAsUp:
    getSupportActionBar().setDisplayHomeAsUpEnabled( true ) ;
    getSupportActionBar().setHomeButtonEnabled( true ) ;
  3. 第一頁:
    mDrawerToggle.setDrawerIndicatorEnabled( truue ) ; // 預設
  4. 第N頁:
    mDrawerToggle.setDrawerIndicatorEnabled( false ) ; 

reference:
http://stackoverflow.com/q/17258020

2015年5月8日 星期五

如何使用全高的側拉抽屜?(DrawerLayout)

目的:


側拉抽屜出來的效果要高度填滿整個螢幕高(同play商店)。

PS.側拉抽屜預設效果會在ActionBar底下滑出


元件:

  1. android.support.v4.widget.DrawerLayout
  2. android.support.v7.widget.Toolbar


操作:

  1. 針對Activity的style 設定<item name="windowActionBar">false</item>
  2. 針對Activity的style 設定<item name="windowActionModeOverlay">true</item>
  3. class中設定setSupportActionBar( toolbar ) ;
**但是這樣getSupportActionBar()就會回傳null**

Q1:

只有一個頁面使用側拉抽屜,但其它頁面不需使用,但需仍需要使用ActionBar。
但是Activity的style中不設定<item name="windowActionBar">false</item>又不行。
若不設定的話,在使用setSupportActionBar( toolbar ) 將會發生Exception。

Error message:
java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.

A1:

  1. 另外設定一個style
  2. 在使用class頁面中的onCreate()的第一行、super.onCreate( savedInstanceState )前,設定額外的style
    setTheme( R.style.另外的style ) ;
  3. 其餘照舊

style code:

    <style name="HomeTheme" parent="AppBaseTheme">
        <item name="windowActionBar">false</item>
        <item name="windowActionModeOverlay">true</item>
    </style>



reference:
http://stackoverflow.com/a/12724687