ym——Android从零开始(20)(TabHost+ExpandableListView)(新)
来源:互联网 发布:iphone离线看书软件 编辑:程序博客网 时间:2024/05/16 06:49
转载请注明本文出自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 能过管理多个Activitypublic 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("第一页")); 要求参数:viewpublic 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?
树形菜单。
0 0
- ym——Android从零开始(20)(TabHost+ExpandableListView)(新)
- ym——Android从零开始(7)(SQLite数据库)(新)
- ym——Android从零开始(9)(ContentProvider内容提供者)(新)
- ym——Android从零开始(17)(多线程下载-上)(新)
- ym——Android从零开始(18)(多线程下载-下)(新)
- ym——Android从零开始(21)(自定义控件)(新)
- ym——Android从零开始(19)(BroadcastReceiver)(新)
- ym——Android从零开始(22)(service、aidl)(新)
- ym——Android从零开始(23)(Notification+ Animation)(新)
- ym——Android从零开始(24)(多媒体播放器)(新)
- ym——Android从零开始(27)(山寨版微信-上)(新)
- ym——Android从零开始(27)(山寨版微信-下)(新)
- ym——Android从零开始(29)(Bluetooth2.0+4.0)(新)
- ym——Android从零开始(30)(WIFI、传感器)(新)
- ym——Android从零开始(31)(AppWidget、快捷图标)(新)
- ym——Android从零开始(32)(WebView)(新)
- ym——Android从零开始(1)(Android体系架构及认识)(新)
- ym——Android从零开始(2)(打电话,发短信,常用布局)(新)
- 【大数类模板】hdoj 4927 Series 1
- struts2示例代码--提交文件
- calculate_dvbt_system_ofdm_output_float_scale_factor
- Linux Mint 17 V2版中文化与输入法
- 第十七天-JavaAPI(Collections、Arrays,静态导入)
- ym——Android从零开始(20)(TabHost+ExpandableListView)(新)
- java 自动拆箱与装箱
- jsp中的路径问题
- LabVIEW串口操作--labview高亮单步显示时串口缓冲区能够正常接收,直接执行时不能接收
- python使用MySQLdb,往库表内添加中文字段要做的修改
- PHP ORM-持久层框架解决方案
- ym——Android从零开始(21)(自定义控件)(新)
- addLoadEvent(func):把多个javascript函数绑定到onload事件
- javascript的设计模式实现05之Composite