【Android】自定义 Tabhost

效果图:


MainActivity.javapackage tianshuai.homePage; import android.app.TabActivity; import android.os.Bundle; import android.view.LayoutInflater; import android.widget.RelativeLayout; import android.widget.TabHost; import android.widget.TabHost.TabSpec; import android.widget.TextView; public class MainActivity extends TabActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //取得当前的TabHost TabHost tabs = getTabHost(); //载入主布局文件 LayoutInflater.from(this).inflate(R.layout.main, tabs.getTabContentView(), true); //给Tab1添加自定义样式 RelativeLayout tabStyle1 = (RelativeLayout)LayoutInflater.from(this).inflate(R.layout.tab_style, null); TextView text1 = (TextView)tabStyle1.findViewById(R.id.tab_label); text1.setText("1"); //给Tab2添加自定义样式 RelativeLayout tabStyle2 = (RelativeLayout)LayoutInflater.from(this).inflate(R.layout.tab_style, null); TextView text2 = (TextView)tabStyle2.findViewById(R.id.tab_label); text2.setText("2"); //创建新Tab, 使用tab1的样式 TabSpec tab1 = tabs.newTabSpec("tab1"); tab1.setIndicator(tabStyle1); tab1.setContent(R.id.Tab1); tabs.addTab(tab1); //创建新Tab, 使用tab2的样式 TabSpec tab2 = tabs.newTabSpec("tab2"); tab2.setIndicator(tabStyle2); tab2.setContent(R.id.Tab2); tabs.addTab(tab2); } }main.xml

table_style.xml tab.xml




转载于:https://www.cnblogs.com/JPAORM/archive/2012/03/29/2510015.html

你可能感兴趣的:(移动开发)