Android开发学习之TabView选项卡详解 -- 基于Android4.4
2014-04-25 15:09
537 查看
直接上代码 -- 基于Android4.4
布局文件
package com.example.viewdemo1; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.widget.TabHost; //新版本中TabActivity过时了,使用一下的方式 public class Tab1Activity extends Activity{ private TabHost myTabHost; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_tab1); myTabHost = (TabHost) findViewById(android.R.id.tabhost); myTabHost.setup();//实例化了tabWidget和tabContent myTabHost.setBackgroundColor(Color.argb(150, 22, 70, 150)); myTabHost.addTab(myTabHost.newTabSpec("腾讯").setIndicator("腾讯",getResources().getDrawable(R.drawable.qq)).setContent(R.id.tab1)); myTabHost.addTab(myTabHost.newTabSpec("华为").setIndicator("华为",getResources().getDrawable(R.drawable.huawei)).setContent(R.id.tab2)); myTabHost.addTab(myTabHost.newTabSpec("百度").setIndicator("百度",getResources().getDrawable(R.drawable.baidu)).setContent(R.id.tab3)); } /* //Android4之前的方式,4之后TabActivity已经过期了 public class Tab1Activity extends android.app.TabActivity{ private TabHost myTabHost; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); myTabHost = this.getTabHost(); LayoutInflater.from(this).inflate(R.layout.activity_tab1, myTabHost.getTabContentView(),true); myTabHost.setBackgroundColor(Color.argb(150, 22, 70, 150)); myTabHost.addTab(myTabHost.newTabSpec("腾讯").setIndicator("腾讯",getResources().getDrawable(R.drawable.qq)).setContent(R.id.tab1)); myTabHost.addTab(myTabHost.newTabSpec("华为").setIndicator("华为",getResources().getDrawable(R.drawable.huawei)).setContent(R.id.tab2)); myTabHost.addTab(myTabHost.newTabSpec("百度").setIndicator("百度",getResources().getDrawable(R.drawable.baidu)).setContent(R.id.tab3)); }*/ @Override public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub return super.onOptionsItemSelected(item); } }
布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TabHost android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" > </TabWidget> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:id="@+id/tab1" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第一个Tab选项卡" /> </LinearLayout> <LinearLayout android:id="@+id/tab2" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第二个Tab选项卡" /> </LinearLayout> <LinearLayout android:id="@+id/tab3" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第三个Tab选项卡" /> </LinearLayout> </FrameLayout> </LinearLayout> </TabHost> </LinearLayout>
相关文章推荐
- Android基于XMPP Smack Openfire下学习开发IM(一)实现用户注册、登录、修改密码和注销等
- Android开发学习之SQLite详解一
- 学习 | Android开发之详解广播机制
- Android基于XMPP Smack Openfire下学习开发IM(二)对分组、好友和头像等一些操作
- Android基于XMPP Smack Openfire下学习开发IM(五)连接断开重连
- Android开发学习之事件处理和Button详解
- Android基于XMPP Smack Openfire下学习开发IM(五)连接断开重连
- 【转载】Android开发学习笔记:Intent的简介以及属性的详解
- Android基于XMPP Smack Openfire下学习开发IM(二)对分组、好友和头像等一些操作
- 关于搭建基于Android和PhoneGap开发环境图文详解
- Android开发学习笔记:5大布局方式详解
- 安卓开发学习之002 LinearLayout之android:layout_gravity详解
- Android开发学习笔记:Intent的简介以及属性的详解
- Android基于XMPP Smack Openfire下学习开发IM(三)会议室创建、加入以及查询会议室中所有成员等
- Android开发学习之基于ZBar实现微信扫一扫
- Android基于XMPP Smack Openfire下学习开发IM(六)总结
- Android基于XMPP Smack Openfire下学习开发IM(三)会议室创建、加入以及查询会议室中所有成员等
- 【转】Android开发学习笔记:5大布局方式详解
- Android基于XMPP Smack Openfire下学习开发IM(四)单人聊天和多人聊天(发送消息、接收消息)
- Android基于XMPP Smack Openfire下学习开发IM(四)单人聊天和多人聊天(发送消息、接收消息)