效果图例如以下:

选项卡的使用:

1.继承TabActivity

2.声明TabHost变量,通过方法getTabHost()获取并赋值。

(TabHost  tabHost =getTabHost() ;)

3.在xml文件里,创建TabHost组件和TabWidget组件。而且TabHost组件的id属性必须是: android:id="@android:id/tabhost" ,

TabWidgett组件的id属性必须是:android:id="@android:id/tabs"。

一共同拥有三个activity,BlogActivity.java、MyTabActivity.java、TestActivity.java。四个xml文件,activity_blog.xml、activity_mytab.xml、activity_test.xml、blog.xml。

MyTabActivity.java

package cn.edu.bzu.micro_blog.activity;

import android.os.Bundle;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.view.Menu;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec; public class MyTabActivity extends TabActivity { //继承TabActivity TabHost tabHost;
TabSpec tabSpec01,tabSpec02;
Intent intent01,intent02;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mytab); tabHost=getTabHost(); //获取tabHost
intent01 = new Intent(this, BlogActivity.class);
intent02 = new Intent(this, TestActivity.class); tabSpec01 = tabHost.newTabSpec("system").setIndicator("Blog",null). //创建选项,选项卡的名称为Blog,
//null的一项是设置选项的图标, 能够通过Resources来获取图片。
setContent(intent01); //设置要显示的页面,也能够是组件。 tabSpec02 = tabHost.newTabSpec("hardware").setIndicator("Test",null).
setContent(intent02); tabHost.addTab(tabSpec01); //加入
tabHost.addTab(tabSpec02);
// 设置默认选中的选项卡为第1个
tabHost.setCurrentTab(0); } @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.first, menu);
return true;
} }

BlogActivity.java

package cn.edu.bzu.micro_blog.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ListView;
import android.widget.SimpleAdapter; public class BlogActivity extends Activity {
private ListView listView;
List<Map<String, ?>> data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_blog); listView = (ListView)findViewById(R.id.lv);
SimpleAdapter simpleAdapter = new SimpleAdapter(BlogActivity.this, getData(), R.layout.blog, new String[]{
"name","address","photo"}, new int[]{R.id.name,R.id.wenzi,R.id.photo}); listView.setAdapter(simpleAdapter); } public List<Map<String, ?>> getData() {
data = new ArrayList<Map<String, ? >>();
Map<String, Object> data01 = new HashMap<String, Object>();
data01.put("name", "张三");
data01.put("address", "近期学习了ListView组件,于是就模仿了一下腾讯微博的样式.看起来效果不错");
data01.put("photo",R.drawable.aa);
data.add(data01);
data01 = new HashMap<String, Object>();
data01.put("name", "李四");
data01.put("address", "仅仅是模仿,全都是硬编码,静态的,谢谢赞赏");
data01.put("photo",R.drawable.th);
data.add(data01);
return data;
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }

TestActivity.java 仅仅有一个TextView。在这我就不上代码了。

activity_mytab.xml

<?

xml version="1.0" encoding="utf-8"?>
<!-- android:id="@android:id/tabhost" 这句是特定的 -->
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" > <RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<!-- android:id="@android:id/tabs"同上 -->
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:gravity="bottom"/> <FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout> </TabHost>

activity_blog.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=".BlogActivity" > <ListView
android:id="@+id/lv"
android:layout_marginTop="50dp"
android:layout_height="match_parent"
android:layout_width="match_parent"> </ListView> </RelativeLayout>

blog.xml

<?

xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <ImageButton
android:id="@+id/photo"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
android:src="@drawable/th" /> <TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@id/photo"
android:text="" /> <TextView
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="230dp"
android:layout_marginTop="20dp"
android:text="1分钟前" /> <TextView
android:id="@+id/wenzi"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_below="@id/name"
android:layout_toRightOf="@id/photo"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:text=""/> </RelativeLayout>

最新文章

  1. 关于IE9-解决background-size的问题
  2. 三维网格细分算法(Catmull-Clark subdivision &amp; Loop subdivision)附源码
  3. JavaScript制作时钟特效
  4. SQL之按两个字段分类汇总
  5. C++ inline
  6. PHP魔术方法小结.md
  7. jquery实现点击按钮滑动到指定位置
  8. leetcode@ [134] Gas station (Dynamic Programming)
  9. js跳转页面代码用法
  10. Tomcat 启动 Debug模式
  11. Oracle表锁住处理
  12. async generator promise异步方案实际运用
  13. NAT穿透解决
  14. JS判断手机还是电脑访问网站
  15. hdu多校第4场 B Harvest of Apples(莫队)
  16. 20172325 2018-2019-2 《Java程序设计》第八周学习总结
  17. Window对象属性
  18. 巧用PHP双$功能兼容线上线下配置文件
  19. ant design的一些坑
  20. 源码安装postgresql数据库

热门文章

  1. 【笔记】PIL 中的 Image 模块
  2. Configure Always On Availability Group for SQL Server on Ubuntu
  3. PHP 判断表单提交的file是否为空
  4. 【LeetCode】Reverse Nodes in k-Group(k个一组翻转链表)
  5. 九度oj 题目1534:数组中第K小的数字
  6. iOS学习笔记08-Quartz2D绘图
  7. BZOJ 2300 [HAOI2011]防线修建 ——计算几何
  8. BZOJ 3998 [TJOI2015]弦论 ——后缀自动机
  9. [BZOJ3054] Rainbow的信号(考虑位运算 + DP?)
  10. 源码分析 脱壳神器ZjDroid工作原理