• 学习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>














  • 相关阅读:
    第一次接触 cef, 遇到不少的问题
    数据结构:并查集
    fel基本使用
    fel兼容多值计算
    CEF3开发者系列之单进程模式应用
    MybatisPlus查询整理
    统一异常/服务处理
    京东工作阶段....
    js 前端使用 prettier格式化scss
    vu3 动态组件
  • 原文地址:https://www.cnblogs.com/gavanwanggw/p/6806596.html
Copyright © 2020-2023  润新知