学习Android之第六个小程序新浪微博(二)(ListView和TabActivity)

效果图例如以下:

选项卡的使用:

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>
时间: 2024-08-02 03:24:46

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

学习Android之第八个小程序文件保存(Notification、AndroidTestCase)

效果图:       .java文件有MainActivity.java.FileService.java.FileServiceTest.java, .xml文件有activity_main.xml. 本次注重AndroidTestCase类的使用,在开发中非常实用.用于测试某一功能. 使用AndroidTestCase类,有如下的要求: 1.在AndroidManifest.xml文件中,<manifest></manifest>中添加如下: <instrumentati

自动批改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.com/android/ADT-22.6.1.zip SDK Tools r22.6版本的下载路径是:http://dl.google.com/android/android-sdk_r22.6-windows.zip SDK Tools r22.6.1版本的下载路径是:http://dl.google

Python小程序练习二之装饰器小例子

Python小程序练习二之装饰器小例子 装饰器: 装饰器实际上就是为了给某程序增添功能,但该程序已经上线或已经被使用,那么就不能大批量的修改源代码,这样是不科学的也是不现实的,因为就产生了装饰器,使得其满足: 1.不能修改被装饰的函数的源代码 2.不能修改被装饰的函数的调用方式 那么根据需求,同时满足了这两点原则,这才是我们的目的. 装饰器的原则组成: < 函数+实参高阶函数+返回值高阶函数+嵌套函数+语法糖 = 装饰器 > 错误例子: 1.1Decorators.py 1 # The aut

微信小程序配置二

tabBar 客户端窗口底部的tab页面切换,只能配置最好两个.最多5个tab 属性说明: 属性 类型 必填 默认值 描述 color HexColor 是 tab上的文字默认颜色 selectedColor HexColor 是 tab上的文字选中时的颜色 backgroundColor HexColor 是 tab的背景颜色 boderStyle String 否 black tab上边框的颜色,仅支持black/white 大专栏  微信小程序配置二> list Array 是 tab的列

学习mpvue : 使用mpvue实现2048小程序

2048 小程序移植版 使用mpvue编写的2048小程序, 仅供交流学习. 图片展示 核心代码 初始化 [00][01][02][03] [10][11][12][13] [20][21][22][23] [30][31][32][33] (例)左移 假设 索引为 => [00][01][02][03] 对应值 => 2 2 0 4 1.建立一个栈 2.从左入栈,如果入栈元素为0, 不做任何处理 否则每入一个栈之前和栈顶元素做对比 如果和栈顶元素不同,入栈 {number: x, flag:

微信小程序(二)框架

配置 配置app.json例子: { "pages": [ "pages/index/index", "pages/logs/index" ], "window": { "navigationBarTitleText": "Demo" }, "tabBar": { "list": [{ "pagePath": "pag

.NET开发微信小程序-生成二维码

1.生成小程序二维码功能 直接请求相应的链接.传递相应的参数 以生成商铺的付款码为例: var shopsId = e.ShopsId //付款码的参数 var codeModel = new function () { } codeModel.path = "pages/PageWxPay/PageWxPay?shopsId=" + shopsId codeModel.width = 430 codeModel.auto_color = false codeModel.line_co

小程序开发二三事--图片错误显示默认图

小程序的image组件不像普通html 的image,没有onerror属性,不过有个binderror回调方法.却不像onerror="this.src='/static/img/fmdefault.png'"来的方便. binderror 当错误发生时,发布到 AppService 的事件名,事件对象event.detail = {errMsg: 'something wrong'}; 当图片错误时,调用binderror方法,然后去改变image的src绑定的值. <ima

微信小程序(二)框架的基本组成

安装好 微信web开发者工具后. 让我们来了解一下工具的框架,上图: 工具总共分为三个部分,左上角红框框: 模拟器:模拟手机小程序界面,模拟器左上角可以选择手机型号,右上角红框处,千万别选成Office 否者会影响后边的运行. 编译器:上图 然后挨着讲解 pages:根目录页 在这里创建页面. 例如 index文件夹,index文件里又可以创建四个文件 后缀分别是.js .json .wxml .wxss 一般页面 只有三个页面 也就是 没有json .js逻辑页面.针对页面数据做出相应的处理