Tab View
For Android Version 4 (Ice Cream Sandwich) have deprecation the TabActivity, In this case i try use the TabHost class but it shows an error,then i use LocalActivityManager class then the problem is solved.
1. MyActivity.java
public class MyActivity extends Activity { TabHost mTabHost; LocalActivityManager mLocalActivityManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.tab_view); mTabHost = (TabHost) findViewById(android.R.id.tabhost); mLocalActivityManager = new LocalActivityManager(this, false); mLocalActivityManager.dispatchCreate(savedInstanceState); mTabHost.setup(mLocalActivityManager); createTab(); } protected void createTab() { setupTab(R.drawable.message_tab_selector,Content1Activity.class); setupTab(R.drawable.group_tab_selector, Content2Activity.class); setupTab(R.drawable.favour_tab_selector,Content3Activity.class); } private void setupTab(int tag, Class<?> className) { View tabview = createTabView(mTabHost.getContext(), tag); Intent intentAndroid = new Intent().setClass(this, className); TabSpec setContent = mTabHost.newTabSpec("").setIndicator(tabview) .setContent(intentAndroid); mTabHost.addTab(setContent); } private static View createTabView(final Context context, int imageid) { View view = LayoutInflater.from(context).inflate(R.layout.tab_bg, null); ImageView tv = (ImageView) view.findViewById(R.id.tabsimage); tv.setImageDrawable(context.getResources().getDrawable(imageid)); return view; } }
2. message_tab_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/message1" android:state_selected="true"/>
<item android:drawable="@drawable/message"/>
</selector>
3.tab_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/inner_content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TabHost
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginLeft="0dip"
android:layout_marginRight="0dip"
android:background="@drawable/bottom_bar" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
</TabHost>
</LinearLayout>
4.tab_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) 2010 Josh Clemm -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabsLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical"
android:padding="10dip" >
<ImageView
android:id="@+id/tabsimage"
android:layout_width="wrap_content"
android:layout_height="40dp" />
</LinearLayout>
Note:
some times the tab will be invisible then we have change add the android:layout_marginBottom="50dp" into the Content layout xml file
No comments:
Post a Comment