转载请注明本文出自Cym的博客(http://blog.csdn.net/cym492224103),谢谢支持!
TabHost
1通过布局显示内容:
Xml:
<?xml version="1.0"encoding="utf-8"?> <TabHostxmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tabHost" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" androidrientation="vertical"> <TabWidget android:id="@android:id/tabs" android提供的规范必须有,相当于标识位 android:layout_width="fill_parent" android:layout_height="wrap_content"/> <FrameLayout android:id="@android:id/tabcontent" android提供的规范 android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:id="@+id/line1" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="one"/> </LinearLayout> <LinearLayout android:id="@+id/line2" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="two"/> </LinearLayout> <LinearLayout android:id="@+id/line3" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="three"/> </LinearLayout> </FrameLayout> </LinearLayout> </TabHost>
Acivity
public class MainActivity extends Activity{ private TabHost mTabHost; /**Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mTabHost = (TabHost) findViewById(R.id.tabHost); //初始化信息 mTabHost.setup(); //创建选项卡 TabSpec tab1 = mTabHost.newTabSpec("tab1"); //头信息 tab1.setIndicator("首页", getResources().getDrawable(R.drawable.i1)); //内容 tab1.setContent(R.id.line1); mTabHost.addTab(tab1); //创建选项卡 TabSpec tab2 = mTabHost.newTabSpec("tab2"); //头信息 tab2.setIndicator("第二页", getResources().getDrawable(R.drawable.i2)); //内容 tab2.setContent(R.id.line2); mTabHost.addTab(tab2); //创建选项卡 TabSpec tab3 = mTabHost.newTabSpec("tab3"); //头信息 tab3.setIndicator("第三页", getResources().getDrawable(R.drawable.i3)); //内容 tab3.setContent(R.id.line3); mTabHost.addTab(tab3); } }
2 通过Acivity显示内容
这样便于维护,适于做逻辑复杂的处理。
Xml
<?xml version="1.0"encoding="utf-8"?> <TabHostxmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tabHost" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" androidrientation="vertical"> <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent"/> </LinearLayout> </TabHost>
Acivity
//ActivityGroup 能过管理多个Activity public class MainActivity2 extends ActivityGroup { private TabHost mTabHost; /**Called when the activity is first created. */ @Override public void onCreate(BundlesavedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main2); mTabHost = (TabHost) findViewById(R.id.tabHost); //初始化信息 mTabHost.setup(getLocalActivityManager()); //创建选项卡 TabSpec tab1 = mTabHost.newTabSpec("tab1"); //头信息 tab1.setIndicator("首页", getResources().getDrawable(R.drawable.i1)); //内容 tab1.setContent(newIntent(this,Activity1.class)); 要求参数:Intent mTabHost.addTab(tab1); //创建选项卡 TabSpec tab2 = mTabHost.newTabSpec("tab2"); //头信息 tab2.setIndicator("第二页", getResources().getDrawable(R.drawable.i2)); //内容 tab2.setContent(new Intent(this,Activity2.class)); mTabHost.addTab(tab2); //创建选项卡 TabSpec tab3 = mTabHost.newTabSpec("tab3"); //头信息 tab3.setIndicator("第三页", getResources().getDrawable(R.drawable.i3)); //内容 tab3.setContent(new Intent(this,Activity3.class)); mTabHost.addTab(tab3); } }
3 自定义头部信息
头部xml
<?xml version="1.0"encoding="utf-8"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" androidrientation="vertical" android:background="@drawable/bg"> <TextView android:id="@+id/tv_info" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:textColor="@android:color/white" android:textSize="22sp"/> </LinearLayout>
颜色选择器xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_selected="true"android:drawable="@drawable/bg_selected"/> <item android:drawable="@drawable/bg_normal"></item> </selector>
Acivity
tab3.setIndicator(createView("第一页")); 要求参数:view public View createView(Stringtext){ LayoutInflatermInflater = getLayoutInflater(); Viewview = mInflater.inflate(R.layout.header, null); TextView tv_info = (TextView)view.findViewById(R.id.tv_info); tv_info.setText(text); return view; }4 不需要布局文件
public class MyTabHostActivity extends TabActivity { @Override protectedvoid onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); 不需要布局文件 只可以管理acitity内容 TabHost tabHost = getTabHost(); } }
ExpandableListView
Xml:
<?xml version="1.0"encoding="utf-8"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" androidrientation="vertical" > <ExpandableListView android:id="@+id/elv" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>Activity
public class MainActivity extends Activity{ /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ExpandableListView elv = (ExpandableListView) findViewById(R.id.elv); File file = new File(getFilesDir(),"commonnum.db"); ///data/data/包名/files if(!file.exists()){ try { //复制数据库 InputStreamis = getResources().getAssets().open("commonnum.db"); 得到数据用流写入///data/data/包名/files便于读取 byte[] buffer = new byte[1024]; int len = 0; OutputStream os = newFileOutputStream(file); while((len = is.read(buffer)) != -1){ os.write(buffer,0, len); } is.close(); os.close(); }catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } List<Map<String,Object>> groupData= DataService.getGroupData(file.getAbsolutePath()); finalList<List<Map<String,Object>>> childData =DataService.getChildData(file.getAbsolutePath()); SimpleExpandableListAdapter adapter = newSimpleExpandableListAdapter( this, 上下文 groupData, 父级数据 android.R.layout.simple_expandable_list_item_1,父级布局 new String[]{"name"}, 父级属性 newint[]{android.R.id.text1}, 父级控件 childData, 子级数据 android.R.layout.simple_expandable_list_item_2,子级布局 newString[]{"name","number"}, 子级属性 newint[]{android.R.id.text1,android.R.id.text2}); 父级控件 elv.setAdapter(adapter); elv.setOnChildClickListener(newExpandableListView.OnChildClickListener() { @Override publicboolean onChildClick(ExpandableListView parent, View v, intgroupPosition, int childPosition, long id) { // TODO Auto-generated method stub //拿到电话号码 String number =(String)childData.get(groupPosition).get(childPosition).get("number"); //复制号码到拨号盘 Intent intent = new Intent(); intent.setAction(Intent.ACTION_DIAL); intent.setData(Uri.parse("tel:"+number)); startActivity(intent); return false; } }); } }
DataService
public class DataService { /** * 得到组数据 * @return */ publicstatic List<Map<String,Object>> getGroupData(String path){ List<Map<String,Object>> list= new ArrayList<Map<String,Object>>(); 打开数据库 SQLiteDatabase db=SQLiteDatabase.openDatabase(path,null, SQLiteDatabase.OPEN_READONLY); if(db.isOpen()){ Cursorc = db.query("classlist", newString[]{"idx","name"}, null,null, null, null, null); while(c.moveToNext()){ Map<String,Object> map = newHashMap<String, Object>(); map.put("idx",c.getString(c.getColumnIndex("idx"))); map.put("name",c.getString(c.getColumnIndex("name"))); list.add(map); } c.close(); db.close(); } return list; } /** *得到子数据 * @param path * @return */ publicstatic List<List<Map<String,Object>>> getChildData(Stringpath){ List<List<Map<String,Object>>>childData = new ArrayList<List<Map<String,Object>>>(); SQLiteDatabase db =SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY); if(db.isOpen()){ //父数据的大小 根据父数据查询子节点数据 List<Map<String,Object>>groupData = getGroupData(path); intsize = groupData.size(); for(inti = 0;i < size;i++){ List<Map<String,Object>> list= new ArrayList<Map<String,Object>>(); Cursor c =db.query("table"+groupData.get(i).get("idx"), newString[]{"name","number"}, null, null, null, null, null); while(c.moveToNext()){ Map<String,Object>map = new HashMap<String, Object>(); map.put("name",c.getString(c.getColumnIndex("name"))); map.put("number",c.getString(c.getColumnIndex("number"))); list.add(map); } c.close(); childData.add(list); } db.close(); } return childData; } }
课后问题
TabHost使用在什么情况?
一个界面显示种不类型的内容时使用。
实现TabHost的两种方式?
一种是自己写布局,一种是直接继承TabAcitity。 得到 TabHost对象
ExpandabledListView?
树形菜单。