Android/Tip&Tech
Android Tab 관련 하단에 붙이는 거랑 인텐트 넘기는 것 팁(ActivityGroup,intent)
행복한 수지아빠
2011. 3. 17. 10:31
출처 : http://rosaria1113.blog.me/110885107
이번에 Tab을 사용해보면서 안것이지만..
안드로이드에서 tab을 사용하는 방법은 정말 많다.
Activity를 상속받고 (Tabhost - TabWidget - FrameLayout).xml을 사용하는 방법도 있고
TabActivity를 상속받아 getTabHost()를 사용하여 TabHost를 가져와서 tab을 추가하는 방법도 있다.
내가 만드는건 하나의 Tab 안에서 사용자의 동작에 따라 해줘야 할게 많을 것 같아서 Tab을 Activity로 구성하는 방법으로 해봤다.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabHost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</FrameLayout>
</LinearLayout>
</TabHost>
|
xml은 위와 같이 TabHost안에 LinearLayout을 두고 그 안에 TabWidget과 하부 화면을 담당할 FrameLayout을 넣었다.
ImageEditorMainActivity.java
package com.ronze.imageeditor;
import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
public class ImageEditorMainActivity extends ActivityGroup {
private TabHost tabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tabHost = (TabHost)findViewById(R.id.tabHost);
// TabHost 를 findViewById 로 생성한 후 Tab 추가 전에 꼭 실행
tabHost.setup(getLocalActivityManager());
tabHost.addTab(tabHost.newTabSpec("Tab_RESIZE")
.setIndicator(getString(R.string.str_tab_resize))
.setContent(new Intent(this, ResizeActivity.class)));
tabHost.addTab(tabHost.newTabSpec("Tab_ROTATE")
.setIndicator(getString(R.string.str_tab_rotate))
.setContent(new Intent(this, RotateActivity.class)));
tabHost.addTab(tabHost.newTabSpec("Tab_WATERMARK")
.setIndicator(getString(R.string.str_tab_watermark))
.setContent(new Intent(this, WatermarkActivity.class)));
}
}
|
Tab 추가 전에 tabHost.setup()을 해야 한다.
만약 activity 호출을 위한 intent를 content로 설정하기 위해서는
tabHost.setup() 대신 tabHost.setup(getLocalActivityManager())을 해야 한다.
그런데 Actvity를 extends 한 경우에는 getLocalActivityManager()가 없다.
왜냐하면
TabActivity extends ActivityGroup extends Activity 이고, getLocalActivityManager()는 ActivityGroup class에 있는 메소드이 이기 때문이다.
따라서 위와 같이 extends ActivityGroup을 해주고 setup을 진행해 줘야 한다.
사실 위 스크린샷과 같이 화면 구성을 할거면 extends TabActivity 를 하여 간단하게 구현할 수 있다.
내가 이리 삽질을 한것은 tab은 하단으로, 화면은 Activity로 구성하고 싶어서다.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabHost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
</FrameLayout>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</TabHost>
|
|