[转帖]Android TabHost的使用_Android, Python及开发编程讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Android, Python及开发编程讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 3182 | 回复: 0   主题: [转帖]Android TabHost的使用        下一篇 
jixiang.lu
注册用户
等级:中尉
经验:474
发帖:34
精华:0
注册:2014-3-17
状态:离线
发送短消息息给jixiang.lu 加好友    发送短消息息给jixiang.lu 发消息
发表于: IP:您无权察看 2014-3-21 9:18:19 | [全部帖] [楼主帖] 楼主

1. 最简单的TabHost,Tab来自于layout下的元素 (只从1个Layout中取数据)

(1)效果图

北京联动北方科技有限公司

(2)代码

1)tab_demo.xml

[xhtml]view plaincopy
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:id="@+id/tab_demo_tv1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="tab_demo_tv1"
/>
<TextView android:id="@+id/tab_demo_tv2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="tab_demo_tv2"
/>
<TextView android:id="@+id/tab_demo_tv3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="tab_demo_tv3"
/>
</FrameLayout>
2)TabDemo.java
[java]view plaincopy
public class TabDemo extends TabActivity {
      private TabHost tabHost;
      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            tabHost = getTabHost();
            LayoutInflater.from(this).inflate(R.layout.tab_demo, tabHost.getTabContentView(), true);
            tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("Tab1", null).setContent(R.id.tab_demo_tv1));
            tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("Tab2", null).setContent(R.id.tab_demo_tv2));
            tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("Tab3", null).setContent(R.id.tab_demo_tv3));
            setContentView(tabHost);
      }
}


2. TabHost绑定动态View(从2个Layout中取数据)

(1)效果图

北京联动北方科技有限公司

(2)代码

1)tab_map.xml

[xhtml]view plaincopy
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/tab_map_id"
>
</FrameLayout>
2)tab_hs.xml
[xhtml]view plaincopy
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/tab_hs_id"
>
<TextView android:id="@+id/tab_hs_tv"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</FrameLayout>
3)MapView.java
[java]view plaincopy
public class MapView extends View {
      public MapView(Context context) {
            super(context);
      }
      protected void onDraw(Canvas canvas) {
            Paint p = new Paint();
            canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.icon), 0, 0, p);
      }
}
4)TabDemo.java
[java]view plaincopy
public class TabDemo extends TabActivity {
      private TabHost tabHost;
      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            tabHost = getTabHost();
            createTabSpec_map();
            createTabSpec_hs();
            setContentView(tabHost);
      }
      private void createTabSpec_map() {
            LayoutInflater inflater_tab1 = LayoutInflater.from(this);
            inflater_tab1.inflate(R.layout.tab_map, tabHost.getTabContentView());
            FrameLayout frameLayout = (FrameLayout) findViewById(R.id.tab_map_id);
            MapView mv = new MapView(this);
            frameLayout.addView(mv);
            TabHost.TabSpec tabSpec_map = tabHost.newTabSpec("map view");
            tabSpec_map.setIndicator("map view", null);
            tabSpec_map.setContent(R.id.tab_map_id); // 动态绑定基于图片的View(通过一个Layout绑定)    
            tabHost.addTab(tabSpec_map);
      }
      private void createTabSpec_hs() {
            LayoutInflater inflater_tab2 = LayoutInflater.from(this);
            inflater_tab2.inflate(R.layout.tab_hs, tabHost.getTabContentView());
            TabHost.TabSpec tabSpec_hs = tabHost.newTabSpec("hs view");
            tabSpec_hs.setIndicator("hs view");
            tabSpec_hs.setContent(R.id.tab_hs_id); // 绑定一个新的Layout    
            tabHost.addTab(tabSpec_hs);
            updateTabSpec_hs();
      }
      private void updateTabSpec_hs() {
            TextView tv = (TextView) findViewById(R.id.tab_hs_tv);
            tv.setText("This is tab2");
      }
}




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论