浏览 6618 次
锁定老帖子 主题:TabActivity 使用
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2010-01-26
TabActivity
[功能] 一个TabActivity 包含 数个标签
[代码 步骤] 1. 得到 TabHost 的实例 TabHost host = this.getTabHost();
2. 定义 CustomTabs 用于填充 TabHost 各个标签布局用 写道
其必须包含 public View createTabContent(String tag) 定义
public class CustomTabs implements TabHost.TabContentFactory { Activity activity; LayoutInflater inflater; LinearLayout layout; public CustomTabs (Activity a) { activity = a; inflater = activity.getLayoutInflater(); } /** {@inheritDoc} */ public View createTabContent(String tag) { activity.setTitle(tag); return addCustomView(tag); } public View addCustomView(String id){ layout = new LinearLayout(activity); layout.setOrientation(LinearLayout.VERTICAL); if(id.equals(TAB1)){ ImageView iv = new ImageView(activity); iv.setImageResource(R.drawable.o); layout.addView(iv, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT)); } else if(id.equals(TAB2)){ EditText edit = new EditText(activity); layout.addView(edit, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); Button btn = new Button(activity); btn.setText("OK"); layout.addView(btn, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)); RadioGroup group = new RadioGroup(activity); group.setOrientation(LinearLayout.HORIZONTAL); RadioButton radio1 = new RadioButton(activity); radio1.setText("Radio 1"); group.addView(radio1); RadioButton radio2 = new RadioButton(activity); radio2.setText("Radio 2"); group.addView(radio2); layout.addView(group, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)); } else if(id.equals(TAB3)){ LinearLayout.LayoutParams param3 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT); layout.addView(inflater.inflate(R.layout.item_3, null),param3); } else if(id.equals(TAB4)){ LinearLayout.LayoutParams param4 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT); layout.addView(inflater.inflate(R.layout.item_4, null),param4); } else { TextView tv5 = new TextView(activity); tv5.setText("no resource aviable!"); tv5.setGravity(1); layout.addView(tv5, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT)); } return layout; } }
3. 使用 CustomTabs host.addTab(host.newTabSpec(TAB1) .setIndicator("Tab 1", getResources().getDrawable(R.drawable.beijing_001_mb5ucom)) .setContent(item));
4. 标签变化的监听函数 host.setOnTabChangedListener(new OnTabChangeListener(){ @Override public void onTabChanged(String tabId) { // TODO Auto-generated method stub } });
5. emulator 运行截图: * 标签1
* 标签2
* 标签3 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2010-11-06
谢谢,最近在学android,这个实例不错
|
|
返回顶楼 | |