2015年3月26日 星期四

[Android] 於Fragment加上自訂義的OptionMenu

如果在很多Fragement頁面中, 想要使用各別不同自訂的OptionMenu, 就必須先將setHasOptionsMenu(ture),  通常會放在onCreate()中, 然後再override onCreateOptionMenu(Menu menu, MenuInflater inflater)
 
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
   getActivity().getMenuInflater().inflate(R.menu.menu_main, menu);
   super.onCreateOptionsMenu(menu, inflater);
}

需注意與在Activity使用, 稍稍不太一樣,差異不大,  僅差在有無回傳值
 
public boolean onCreateOptionsMenu(Menu menu) {
  getMenuInflater().inflate(R.menu.menu_main, menu);
  return super.onCreateOptionsMenu(menu);
}

因為Fragment是依附在Activity上, 因此我們必須用getActivity()來取得Context,
才能夠使用Inflater這個東東。


最後我們就可以覆寫onOptionsItemSelected(MenuItem item)來撰寫各個item的處理事件
 
public boolean onOptionsItemSelected(MenuItem item) {
  ......
  return super.onOptionsItemSelected(item);
}

[Android] Screen rotate issue

這幾天寫App時發現開啟螢幕自動旋轉的功能, 會因為Activity生命週期特性
Activity running -> onPause() -> onStop()->onDestroy()->onCreate()->onStrat()->onResume()->Activity running
Activity會被整個Destroy再重建, 如果畫面有些執行中的東東, 都會被清空到最初始的狀態。
Google後發現有個可暫時頂著用的方法, 但是Google有說這個方法是下下策(有空再找其他方法了XD)

AndroidManifrest.xmlActivity標籤中加入
android:configChanges="orientation|keyboardHidden|screenSize"

screenSize是在API level 13加入的, 以下說明:
Beginning with Android 3.2 (API level 13), the "screen size" also changes when the device switches between portrait and landscape orientation. Thus, if you want to prevent runtime restarts due to orientation change when developing for API level 13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes), you must include the "screenSize" value in addition to the "orientation" value. That is, you must declare android:configChanges="orientation|screenSize". However, if your application targets API level 12 or lower, then your activity always handles this configuration change itself (this configuration change does not restart your activity, even when running on an Android 3.2 or higher device).

2015年3月9日 星期一

[Android] 官網訓練教學

雖然有點Lag了, 但是找到這個還是很開心, 與其在網路上大海撈針的找文章, 還是先把Google出的訓練文件看一看吧, 裡面有很多細節是一般文章有忽略的喔!!

http://developer.android.com/training/index.html

2015年3月5日 星期四

[Android] GridView練習

看網路上的教學寫了一個小程式, 利用GridView列出所有在Drawable裡面的圖片(這裡放入12生肖圖), 點入其中一張圖跳轉到另一個Activity顯示完整的圖, 原本是想在這個Activity做Crop的動作, 但是還在找相關的教學文章, 所以還沒有implement


MainActivity.java
public class MainActivity extends ActionBarActivity {
    private GridView gridView;
    public static int[] image = {
            R.drawable.mouse,
            R.drawable.cow,
            R.drawable.tiger,
            R.drawable.rabbit,
            R.drawable.dragon,
            R.drawable.snake,
            R.drawable.horse,
            R.drawable.goat,
            R.drawable.monkey,
            R.drawable.chicken,
            R.drawable.dog,
            R.drawable.pig
    };

    private String[] imgText = {"鼠","牛","虎","兔","龍","蛇","馬","羊","猴","雞","狗","豬"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        List> items = new ArrayList>();
        for(int i =0;i < imgText.length;i++)
        {
            Map item = new HashMap();
            item.put("image", image[i]);
            item.put("text",imgText[i]);
            items.add(item);
        }

        SimpleAdapter adapter = new SimpleAdapter(this, items,R.layout.grid_item, new String[]{"image","text"}, new int[]{R.id.image,R.id.text});

        gridView = (GridView)findViewById(R.id.main_page_gridview);
        gridView.setNumColumns(3);
        gridView.setAdapter(adapter);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {
                Toast.makeText(getApplicationContext(),"Your choice is "+imgText[position],Toast.LENGTH_SHORT).show();
                view.setBackgroundColor(Color.BLUE);
                Intent it = new Intent(MainActivity.this, FullImage.class);
                it.putExtra("img",position);
                startActivity(it);
            }
        });

    }



FullImage.java
public class FullImage extends ActionBarActivity {
    ImageView image;
    int image_index;
    Drawable drawable;
    Resources res;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_full_image);

        image = (ImageView)findViewById(R.id.image);
        image_index = getIntent().getExtras().getInt("img");

        res = getBaseContext().getResources();
        drawable = res.getDrawable(MainActivity.image[image_index]);

        image.setImageDrawable(drawable);
    }

activity_main.xml

<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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity">
 <GridView android:id="@+id/main_page_gridview"
 android:numColumns="auto_fit"
 android:gravity="center"
 android:columnWidth="100dp"
 android:stretchMode="columnWidth"
 android:layout_width="match_parent"
 android:layout_height="match_parent" />
 </LinearLayout>

activity_full_image.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"
android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context="com.foxlinkimage.alex.gridviewtest.FullImage"
 android:id="@+id/relayout">
 <ImageView android:id="@+id/image"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" />
 <LinearLayout android:layout_below="@+id/image"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content">
 <Button android:id="@+id/crop"
 android:text="Crop"
 android:textSize="15sp"
 android:layout_width="80dp"
 android:layout_height="40dp" />
 </LinearLayout>
 </RelativeLayout>


grid_item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:gravity="center" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/image" android:layout_marginTop="10dp" android:layout_width="100dp" android:layout_height="100dp" /> <TextView android:id="@+id/text" android:textSize="12sp" android:gravity="center" android:paddingTop="10dp" android:paddingBottom="10dp" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>