效果图例如以下:

选项卡的使用:

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>

学习Android之第六个小程序新浪微博(二)(ListView和TabActivity)的更多相关文章

  1. 自动批改android模拟器的imei的小程序 和 下载各个版本SDK Tools及ADT

    ADT 22.6.0版本的下载路径是:http://dl.google.com/android/ADT-22.6.0.zip ADT22.6.1版本的下载路径是:http://dl.google.co ...

  2. 关于微信小程序获取二维码的踩坑记录

    1.踩坑需求:获取小程序的二维码 2.踩坑接口: https://api.weixin.qq.com/wxa/getwxacode?access_token=ACCESS_TOKEN3 踩坑代码 pu ...

  3. 微信小程序配置二

    tabBar 客户端窗口底部的tab页面切换,只能配置最好两个.最多5个tab 属性说明: 属性 类型 必填 默认值 描述 color HexColor 是 tab上的文字默认颜色 selectedC ...

  4. 手持式停车收费管理系统全套案例,支持车牌识别,包含了android版app,微信小程序查询,响应式管理后台,云端大数据存储

    先展示几个app效果图片吧,使用起来非常方便,关联了机器的快捷键操作,操作速度提高了不少,摄像头车牌自动识别,车牌识别无网络情况下离线也可以使用   再来一张后台截图,停车场信息完整显示,今日数据实时 ...

  5. 微信小程序学习笔记(1)-微信小程序样式设置逻辑

    1.微信小程序的样式设置统一在每一页的.wxss的样式文件中,所有的样式设置代码统一写入这个文件中: 2.样式主要是通过.wxml里面控件的“class”属性来调用,此处调用会有几个细节要注意: 1) ...

  6. 小程序学习三 一切的开始app() 小程序的注册

    现在打开 app.js //app.js App({ onLaunch(options) { //小程序初始化 // console.log("小程序初始化", options) ...

  7. 微信小程序入门二

    # 微信小程序开发实战 ## 准备 ### 课程概要 - 微信小程序基本介绍- 开发环境及工具的安装配置- 微信小程序的设计规范- 微信小程序基本结构分析- WXML和WXSS语法规范- 微信小程序A ...

  8. 微信小程序测试二三事

    微信小程序虽是微信推出的新形态的产品,但是在测试思路上跟其他的传统测试,大相径庭.既然大相径庭,那我们该如何测试呢. 功能测试功能测试跟传统的web功能测试一样,不是app的功能测试哦.这是因为小程序 ...

  9. 微信小程序笔记<二>认识app.json

    *.json文件在小程序开发中必不可少,从 app.json 开始认识小程序中的配置文件*.json: app.json 为小程序必须文件,它不仅作为配置文件管理着小程序的UI还充当着路由器的功能: ...

随机推荐

  1. ACM-ICPC 2018 沈阳赛区网络预赛 K. Supreme Number

    A prime number (or a prime) is a natural number greater than 11 that cannot be formed by multiplying ...

  2. lombok安装、配置、使用

    eclipse安装Lombok 运行安装: java -jar ${path}\lombok.jar 选择IDE所在路径点击Install/Update即可使用. 手动安装1. 将lombok.jar ...

  3. skkyk:线段树浅谈

    推荐前辈学姐博客文章,写的很细 https://www.cnblogs.com/TheRoadToTheGold/p/6254255.html 学学半,此随笔主要是加深自己对线段树的理解 题目:洛谷P ...

  4. NYOJ 469 擅长排列的小明 II

    擅长排列的小明 II 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 小明十分聪明,而且十分擅长排列计算. 有一天小明心血来潮想考考你,他给了你一个正整数n,序列1, ...

  5. Leetcode 376.摆动序列

    摆动序列 如果连续数字之间的差严格地在正数和负数之间交替,则数字序列称为摆动序列.第一个差(如果存在的话)可能是正数或负数.少于两个元素的序列也是摆动序列. 例如, [1,7,4,9,2,5] 是一个 ...

  6. POJ 1971-Parallelogram Counting,暴力1063ms!

    Parallelogram Counting 刚学hash还不会用,看到5000ms的时限于是想着暴力来一发应该可以过.以前做过类似的题,求平行四边形个数,好像是在CF上做的,但忘了时限是多少了,方法 ...

  7. 04-offsetLeft和style.left的区别

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  8. Spring Cloud 从入门到精通

    Spring Cloud 是一套完整的微服务解决方案,基于 Spring Boot 框架,准确的说,它不是一个框架,而是一个大的容器,它将市面上较好的微服务框架集成进来,从而简化了开发者的代码量. 本 ...

  9. POJ 2187 Beauty Contest ——计算几何

    貌似直接求出凸包之后$O(n^2)$暴力就可以了? #include <cstdio> #include <cstring> #include <string> # ...

  10. BZOJ 4589 Hard Nim ——FWT

    [题目分析] 位运算下的卷积问题. FWT直接做. 但还是不太民白,发明者要承担泽任的. [代码] #include <cstdio> #include <cstring> # ...