Android学习第二十天----tabHost应用
tabhost有两种创建方式,第一种,在activity中继承TabActivity类,然后编写如下的代码就可以创建tabhost
package com.example.tabhosttest; import android.os.Bundle; import android.app.TabActivity; import android.content.Intent; import android.widget.RadioGroup; import android.widget.RadioGroup.OnCheckedChangeListener; import android.widget.TabHost; import android.widget.TabHost.TabSpec; public class MainActivity extends TabActivity { private TabHost mTabHost; private RadioGroup mRadioGroup; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mTabHost = this.getTabHost(); TabSpec mtaSpec1 = mTabHost.newTabSpec("HOME").setIndicator("home"); mtaSpec1.setContent(new Intent(MainActivity.this,HOME.class)); mTabHost.addTab(mtaSpec1); TabSpec mtaSpec2 = mTabHost.newTabSpec("ADDR").setIndicator("addr"); mtaSpec2.setContent(new Intent(MainActivity.this,ADDR.class)); mTabHost.addTab(mtaSpec2); TabSpec mtaSpec3 = mTabHost.newTabSpec("INFO").setIndicator("info"); mtaSpec3.setContent(new Intent(MainActivity.this,INFO.class)); mTabHost.addTab(mtaSpec3); TabSpec mtaSpec4 = mTabHost.newTabSpec("MORE").setIndicator("more"); mtaSpec4.setContent(new Intent(MainActivity.this,MORE.class)); mTabHost.addTab(mtaSpec4); } }
该tabhost需要4个页面,因此也就需要4个activity和4个布局文件,文件名可以随便取,但是要符合命名规则。
newTabSper("HOME")这个是一个标识,不在页面中显示,在页面中显示的是setIndicator("home");这里的home信息
在intent中,只要后面的跳转activity写上自己定义的4个,然后就可以了。这样显示的效果,这些按钮显示在屏幕上方的,如何显示在屏幕下方?
在以上的基础之上,做如下修改,
xml中,
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="@string/hello_world" /> <TabHost android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" > </FrameLayout> <TabWidget android:id="@android:id/tabs" android:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="gone" > </TabWidget> <RadioGroup android:id="@+id/radiogroup" android:layout_width="match_parent" android:layout_height="80dp" android:orientation="horizontal" > <RadioButton android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/button1" android:layout_weight="1" android:background="#ff000f" android:gravity="center" android:button="@null" android:text="home" /> <RadioButton android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/button2" android:layout_weight="1" android:background="#ff9999" android:button="@null" android:gravity="center" android:text="info" /> <RadioButton android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/button3" android:layout_weight="1" android:background="#88880f" android:button="@null" android:gravity="center" android:text="addr" /> <RadioButton android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/button4" android:layout_weight="1" android:background="#f22222" android:gravity="center" android:button="@null" android:text="more" /> </RadioGroup> </LinearLayout> </TabHost> </RelativeLayout>
java文件中,
package com.example.tabhosttest; import android.os.Bundle; import android.app.TabActivity; import android.content.Intent; import android.widget.RadioGroup; import android.widget.RadioGroup.OnCheckedChangeListener; import android.widget.TabHost; import android.widget.TabHost.TabSpec; public class MainActivity extends TabActivity { private TabHost mTabHost; private RadioGroup mRadioGroup; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTabHost = this.getTabHost(); TabSpec mtaSpec1 = mTabHost.newTabSpec("HOME").setIndicator("home"); mtaSpec1.setContent(new Intent(MainActivity.this,HOME.class)); mTabHost.addTab(mtaSpec1); TabSpec mtaSpec2 = mTabHost.newTabSpec("ADDR").setIndicator("addr"); mtaSpec2.setContent(new Intent(MainActivity.this,ADDR.class)); mTabHost.addTab(mtaSpec2); TabSpec mtaSpec3 = mTabHost.newTabSpec("INFO").setIndicator("info"); mtaSpec3.setContent(new Intent(MainActivity.this,INFO.class)); mTabHost.addTab(mtaSpec3); TabSpec mtaSpec4 = mTabHost.newTabSpec("MORE").setIndicator("more"); mtaSpec4.setContent(new Intent(MainActivity.this,MORE.class)); mTabHost.addTab(mtaSpec4); mRadioGroup = (RadioGroup)findViewById(R.id.radiogroup); mRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId) { case R.id.button1: mTabHost.setCurrentTab(0); break; case R.id.button2: mTabHost.setCurrentTab(1); break; case R.id.button3: mTabHost.setCurrentTab(2); break; case R.id.button4: mTabHost.setCurrentTab(3); break; default : break; } } }); } }
这样就可以将图标放置在屏幕下方的位置上。
在监听器上还有一个方法,每个case中的,
mTabHost.setCurrentTab(3);
可以换成
mTabHost.setCurrentTabByTag("HOME");
效果是一样的,上面的“HOME”信息就是这样用的。