TabHost使用简介
2014-11-06 15:10
162 查看
TabActivity 类已经被放弃,所以只继承Activity就可以
main.xml
string.xml
MainActivity.java
main.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" android:background="@color/white" > <TabHost android:id="@+id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginTop="14dp" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <!-- TabWidget的id,建议不要修改 --> <TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" > </TabWidget> <!-- FrameLayout的id,建议不要修改 --> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="match_parent" > <!-- 可使用LinearLayout等布局设置其他控件 --> <!-- 标签1 --> <TextView android:id="@+id/tab1" android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/first" /> <!-- 标签2 --> <TextView android:id="@+id/tab2" android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/second" /> <!-- 标签3 --> <TextView android:id="@+id/tab3" android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/third" /> </FrameLayout> </LinearLayout> </TabHost> </RelativeLayout>
string.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">TabHost</string> <string name="action_settings">Settings</string> <string name="first">我是第一选项卡啊啊啊</string> <string name="second">我是第二选项卡啊啊啊</string> <string name="third">我是第三选项卡啊啊啊</string> </resources>
MainActivity.java
private TabHost th; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); th = (TabHost) findViewById(R.id.tabhost); th.setup(); //取得TabWidget,以便修改标签内容 TabWidget tabWidget = th.getTabWidget(); //添加三个标签 th.addTab(th.newTabSpec("tab_1") .setIndicator("选项1", getResources().getDrawable(R.drawable.ic_launcher)) .setContent(R.id.tab1)); th.addTab(th.newTabSpec("tab_2") .setIndicator("选项2", getResources().getDrawable(R.drawable.ic_launcher)) .setContent(R.id.tab2)); th.addTab(th.newTabSpec("tab_3") .setIndicator("选项3", getResources().getDrawable(R.drawable.ic_launcher)) .setContent(R.id.tab3)); //可设置TabWidget背景及布局等 // tabWidget.getChildAt(1).setBackgroundColor(getResources().getColor(R.color.red)); // tabWidget.getChildAt(0).setLayoutParams(new // LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, // LayoutParams.MATCH_PARENT)); //标签图标为ImageView 对象,android.R.id.icon 为第一个标签的图片android.R.id.icon1为第二个标签图片 ImageView img1 = (ImageView) tabWidget.getChildAt(0).findViewById(android.R.id.icon); img1.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); //可设置图片属性及更换图片 img1.setImageDrawable(getResources().getDrawable(R.drawable.lgn0006)); //标签文字为TextView 对象,android.R.id.title 为第一个标签的文字android.R.id.title1为第二个标签文字 //可更改颜色字体等 TextView textview = (TextView) tabWidget.getChildAt(0).findViewById(android.R.id.title); textview.setTextSize(10); }
相关文章推荐
- Android中关于TabHost的使用简介
- 简介使用ASP.NET访问Oracle数据库的方法
- linux环境下的"蚂蚁"-wget使用简介
- TreeCellRenderer使用方法简介
- 消息队列(Message Queue)简介及其使用
- SSH简介及配置使用SSH登陆远程主机
- 使用委托数组的一个例子简介一下委托
- 使用 Microsoft Visual C# 2005 Express Edition 编程简介
- .Net服务组件(ServicedComponent)简介及其使用
- DOM4J 使用简介(转)
- AWStats: 跨平台的日志分析工具使用简介
- 简介使用ASP.NET访问Oracle数据库的方法
- [Struts]使用StrutsTestCase对Action进行单元测试简介
- AWStats: Apache/IIS的日志分析工具——在GNU/Linux和Windows平台上的使用简介
- 简介使用ASP.NET访问Oracle数据库的方法
- DOM4J 使用简介
- 域安全通道实用工具nltest.exe的使用简介(下)
- 简介使用ASP.NET访问Oracle数据库的方法
- DOM4J 使用简介[转载]
- 简介使用ASP.NET访问Oracle数据库的方法