Android Tabs Views
I have decided to use views for my tab interface... I follow a tab interface tutorial and the result of this tutorial is 4 tabs without content (text) under my tabs..
I was wondering how views working.. how can I make a method to set content to tab from an another class.. So main.java is my main file with the views (tabs). Tab1.java has google maps navigate code.
How I can invoke the method setupTab and set the navigation function to tab 1.
Here you can see my code:
Thanks in advance! package CustomTabs;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.TabHost.TabContentFactory;
import android.widget.TabHost.TabSpec;
public class CustomTabs extends Activity {
private TabHost mTabHost;
private void setupTabHost() {
mTabHost = (TabHost) findViewById(android.R.id.tabhost);
mTabHost.setup();
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// construct the tabhost
setContentView(R.layout.main);
setupTabHost();
setupTab(new TextView(this), "Tab 1", getResources().getDrawable(R.drawable.ic_tab_artists));
setupTab(new TextView(this), "Tab 2", getResources().getDrawable(R.drawable.ic_tab_artists));
setupTab(new TextView(this), "Tab 3", getResources().getDrawable(R.drawable.ic_tab_artists));
setupTab(new TextView(this), "Tab 4", getResources().getDrawable(R.drawable.ic_tab_artists));
{
final View v = mTabHost.getTabWidget().getChildAt(0);
v.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_bg_selector));
TextView tv = (TextView) v.findViewById(android.R.id.title);
tv.setTextColor(this.getResources().getColorStateList(R.drawable.tab_text_selector));
}
{
final View v = mTabHost.getTabWidget().getChildAt(1);
v.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_bg_selector));
TextView tv = (TextView) v.findViewById(android.R.id.title);
tv.setTextColor(this.getResources().getColorStateList(R.drawable.tab_text_selector));
}
{
final View v = mTabHost.getTabWidget().getChildAt(2);
v.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_bg_selector));
TextView tv = (TextView) v.findViewById(android.R.id.title);
tv.setTextColor(this.getResources().getColorStateList(R.drawable.tab_text_selector));
}
{
final View v = mTabHost.getTabWidget().getChildAt(3);
v.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_bg_selector));
TextView tv = (TextView) v.findViewById(android.R.id.title);
tv.setTextColor(this.getResources().getColorStateList(R.drawable.tab_text_selector));
}
}
private void setupTab(final View view, final String tag, Drawable icon) {
TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tag, icon).setConten开发者_JAVA百科t(new TabContentFactory() {
public View createTabContent(String tag) {return view;}
});
mTabHost.addTab(setContent);
}
}
You need to create some Activity to do that. My sample took 2 Activity, first Activity will extends TabActivity and the second one will extends Activity.
Here is my simple sample :
- Activity that extends TabActivity
package com.bsoft.activity;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import com.bsoft.shared.Shared;
public class ViewActivity extends TabActivity {
@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
String[] strVal = this.getIntent().getExtras().getString("content").split(Shared.SPACE);
setTitle(strVal[1]);
Intent paliIntent = new Intent(this, PaliActivity.class);
paliIntent.putExtra("val", strVal[2]);
Intent indonesiaIntent = new Intent(this, IndonesiaActivity.class);
indonesiaIntent.putExtra("val", strVal[3]);
TabHost tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("one").setIndicator("Pali").setContent(paliIntent));
tabHost.addTab(tabHost.newTabSpec("two").setIndicator("Indonesia").setContent(indonesiaIntent));
}
}
- Another Activity :
package com.bsoft.activity;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import com.bsoft.shared.Shared;
public class IndonesiaActivity extends Activity {
@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.indonesia);
String val = this.getIntent().getExtras().getString("val");
WebView indonesiaContent = (WebView) findViewById(R.id.wvIndoContent);
indonesiaContent.loadData("" + val.replace("\n", "
") + "", Shared.MIME_TYPE, Shared.ENCODING);
Shared s = new Shared();
s.SetZoomControls(indonesiaContent, this);
}
}
精彩评论