2013年8月23日星期五

新建的Android程式,預設無Menu bar (Action bar)

最近新建Android程式,發現Theme等背景都幫你設好了,
原本新建好的Android背景是黑的,
但新近建的Android程式,背景是白的;

查了一下,
原本是在 AndroidManifest.xml中多了android:theme="@style/AppTheme"

 <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

反正是程式自行建立的,所以應該不是壞事,
結果問題來了,在Activity中的onCreateOptionsMenu竟然跑不進去,



@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
Log.d(TAG, "onCreateOptionsMenu");
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

在Log裡面居然看不到它印出onCreateOptionsMenu的Log,
趕緊上網路查查去;
原來要修改style.xml(這也是程式自己產生的)

<resources>

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppBaseTheme" parent="android:Theme.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

</resources>



原本的Theme使用是android:Theme.Light是不會產生Menu bar(Action bar)的,
要用android:Theme.Holo.Light來取代,修改如下:


<resources>

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppBaseTheme" parent="android:Theme.Holo.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

</resources>


這樣程式就會正常了。

2013年8月11日星期日

cakephp Facebook plugin FB.login callback 問題(已解決)

最近因工作需要,開始接觸cakephp用的FB  Plugin,
網路上找到的plugin:
webtechnick/CakePHP-Facebook-Plugin
網址:https://github.com/webtechnick/CakePHP-Facebook-Plugin

透過該網站的說明,將Plugin裝進去後,
第一關(登入)就卡關:

原程式碼:

echo $this->Facebook->login(array(
'perms' => 'email,read_stream,publish_stream',
'id'=>'loginButton',
'show-faces'=>false,
'status'=>'login',
));

狀況:
登入順利進行,但...怎麼登入後沒有導向我指定的網址?
設定網站說明的afterFacebookLogin callback也沒有用,
多加一個參數redirect後:

echo $this->Facebook->login(array(
'perms' => 'email,read_stream,publish_stream',
'id'=>'loginButton',
'show-faces'=>false,
'status'=>'login',
'redirect' => array('controller' => 'frontend', 'action' => 'login'),
));

原本看得到的登入icon,竟然不見了...
這個問題卡了二天,最後才找到解決方法;
根據
的說法,設定redirect參數是有用的,但設定後會發生login icon不見的問題,
所以要手動再設定img參數,如下方程式碼:

echo $this->Facebook->login(array(
'perms' => 'email,read_stream,publish_stream',
'id'=>'loginButton',
'show-faces'=>false,
'status'=>'login',
'redirect' => array('controller' => 'frontend', 'action' => 'login'),
'img'=>'Menubtno.png',
));

圖片因為已不是用原先FB的login圖片了,所以要自己做一張,
放在webroot/Facebook/img下,以上方的程式碼來說,
我有放一張圖在webroot/Facebook/img/Menubtno.png,
按下登入後,終於會導到我的login頁面了。

卡了二天,終於闖過第一關了。

2013年8月3日星期六

Android﹣從String取得Resources對應的int

寫Android程式時,在取Resources資源時,
正常情況不會知道對應的int,但一定會知道寫在String.xml中的name;

但因為這些name在編輯時會轉成JAVA的Field,放在R.java檔裡面,
所以取Resources資源時,必需透過類似R.string.name的方式,才能取到對應的int;

但世事不是都這麼美好的,有時name是在程式執行中才能拿到的,
而拿到手的是一個String,此時需要想辦法轉成R.string.name對應的int,
才能取對對應的,但要怎麼轉呢?

透過reflect的方式,下方是透過String取得Drawable的int範例程式碼:


public int getDrawableId(String id) throws SecurityException,NoSuchFieldException, IllegalArgumentException,IllegalAccessException {
Field f = (Field) R.drawable.class.getDeclaredField(id);
return f.getInt(R.drawable.class);
}