Android布局--TabHost的使用
来源:互联网 发布:小米2s4g网络怎么设置 编辑:程序博客网 时间:2024/05/16 04:59
1.Tab来自于layout下的元素
(1)效果图
(2)代码
1)tab_demo.xml
- <?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
- public
class TabDemo extendsTabActivity { -
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
- <?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 extendsView - {
-
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
- public
class TabDemo extendsTabActivity { -
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" -
} - }
- Android布局--TabHost的使用
- Android TabHost的使用
- Android TabHost的使用
- Android TabHost的使用
- Android TabHost的使用
- android Tabhost的使用
- Android TabHost的使用 .
- Android TabHost的使用
- Android TabHost的使用
- android的Tabhost使用
- Android TabHost的使用
- android TabHost的两种布局方式
- Android TabHost布局
- Android之TabHost布局
- Android之TabHost布局
- android tabHost布局 大全
- Android之TabHost布局
- Android之TabHost布局
- Eclipse快捷键
- 甲骨文公司老板埃里森在耶如大学的…
- Android--DataPicker用法
- 正则表达式
- java常用布局
- Android布局--TabHost的使用
- 记住,你即将死去!——乔布斯
- Android语音识别--RecognizerInten…
- Android TTS语音识别
- 设计模式--工厂模式Factory
- Photoshop常用快捷键
- Android--TabHost的两种实现方法
- Eclipse格式化代码块快捷键失灵解…
- Android-SharedPreferences用法