android学习:模拟微博看看界面
生活随笔
收集整理的這篇文章主要介紹了
android学习:模拟微博看看界面
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
這幾天學會了ListView組件,這個組件真的很有用。希望在今后的開發中能大量用到。
<span?style="font-family:Comic?Sans?MS;font-size:12px;"><?xml?version="1.0"?encoding="utf-8"?>?? <LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"?? ????android:id="@+id/liner"?? ????android:layout_width="fill_parent"?? ????android:layout_height="fill_parent"?? ????android:orientation="vertical"?>?? ????<LinearLayout??? ????????android:layout_width="match_parent"?? ????????android:layout_height="wrap_content"?? ????????android:padding="6dip"?? ????????android:background="#CD4F39">?? ????????<TextView??? ????????????android:layout_width="wrap_content"?? ????????????android:layout_height="wrap_content"?? ????????????android:text="新浪微博-隨便看看"?? ????????????android:textSize="15sp"?? ?????????????? ????????????android:textColor="@android:color/white"?? ????????????/>?? ????</LinearLayout>?? ?? ????<com.bzu.edu.gxs.view.MyListView?? ????????android:id="@+id/myListView1"?? ????????android:layout_width="match_parent"?? ????????android:layout_height="wrap_content"?>?? ????</com.bzu.edu.gxs.view.MyListView>?? </LinearLayout></span>??
??????
<span?style="font-family:Comic?Sans?MS;font-size:12px;"><?xml?version="1.0"?encoding="utf-8"?>?? <LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"?? ????android:layout_width="match_parent"?? ????android:layout_height="match_parent"?? ????android:padding="10dip"?? ????android:orientation="vertical"?>?? ????<RelativeLayout??? ????????android:layout_width="match_parent"?? ????????android:layout_height="wrap_content"?? ????????>?? ????????<ImageView??? ????????????android:id="@+id/head_img"?? ????????????android:src="@drawable/p1"?? ????????????android:layout_width="wrap_content"?? ????????????android:layout_height="wrap_content"?? ????????????android:layout_marginTop="5dip"?? ????????????android:layout_alignParentLeft="true"?? ????????????/>?? ????????<LinearLayout??? ????????????android:layout_width="match_parent"?? ????????????android:layout_height="wrap_content"?? ????????????android:layout_toRightOf="@+id/head_img"?? ????????????android:layout_marginLeft="7dip"?? ????????????android:orientation="vertical">?? ????????????<RelativeLayout??? ????????????????android:layout_width="match_parent"?? ????????????????android:layout_height="wrap_content">?? ????????????????<TextView??? ????????????????????android:id="@+id/name"?? ????????????????????android:layout_width="wrap_content"?? ????????????????????android:layout_height="wrap_content"?? ????????????????????android:layout_alignParentLeft="true"?? ????????????????????android:textColor="#242424"?? ????????????????????android:textSize="14sp"?? ????????????????????android:typeface="sans"?? ????????????????????android:textStyle="bold"?? ????????????????????android:text="@string/gxs"/>?? ????????????????<TextView??? ????????????????????android:id="@+id/time"?? ????????????????????android:layout_width="wrap_content"?? ????????????????????android:layout_height="wrap_content"?? ????????????????????android:layout_toRightOf="@id/name"?? ????????????????????android:layout_alignBottom="@id/name"?? ????????????????????android:layout_marginLeft="5dip"?? ????????????????????android:textColor="#FFFFFF"?? ????????????????????android:textSize="12sp"?? ????????????????????android:text=""/>?? ?????????????????? ?????????????????<TextView??? ????????????????????android:id="@+id/rq"?? ????????????????????android:layout_width="wrap_content"?? ????????????????????android:layout_height="wrap_content"?? ????????????????????android:layout_alignParentRight="true"?? ????????????????????android:layout_centerVertical="true"?? ????????????????????android:textSize="13sp"?? ????????????????????android:text="1分鐘前"/>?? ????????????</RelativeLayout>?? ????????????<LinearLayout??? ????????????????android:layout_width="match_parent"?? ????????????????android:layout_height="wrap_content"?? ????????????????android:layout_marginTop="5dip">?? ????????????????<TextView??? ????????????????????android:id="@+id/article"?? ????????????????????android:layout_width="match_parent"?? ????????????????????android:layout_height="wrap_content"?? ????????????????????android:minHeight="50px"?? ????????????????????android:textSize="12sp"?? ????????????????????android:text="gxs"/>?? ????????????</LinearLayout>?? ????????</LinearLayout>?? ????</RelativeLayout>?? ?????? ?? </LinearLayout>?? </span>??
5.java代碼
<span?style="font-family:Comic?Sans?MS;font-size:12px;">package?com.bzu.gxs;?? ?? import?java.text.SimpleDateFormat;?? import?java.util.ArrayList;?? import?java.util.Date;?? import?java.util.List;?? import?java.util.Random;?? import?com.bzu.edu.gxs.adapter.ArticleAdapter;?? import?com.bzu.edu.gxs.adapter.Messages;?? import?com.bzu.edu.gxs.view.MyListView;?? import?com.bzu.gxs.*;?? import?android.R.integer;?? import?android.os.Bundle;?? import?android.os.Handler;?? import?android.os.Message;?? ?? ?? import?android.app.Activity;?? import?android.content.res.TypedArray;?? import?android.view.Menu;?? import?android.view.Window;?? import?android.widget.ArrayAdapter;?? import?android.widget.BaseAdapter;?? import?android.widget.LinearLayout;?? ?? public?class?MainActivity?extends?Activity?{?? ?????? ????private?MyListView?mylistview=null;?? ????private?List<Messages>?list_msg=null;?? ????private?BaseAdapter?adapter=null;?? ????@Override?? ????protected?void?onCreate(Bundle?savedInstanceState)?{?? ????????super.onCreate(savedInstanceState);?? ????????requestWindowFeature(Window.FEATURE_NO_TITLE);?? ????????setContentView(R.layout.activity_main);?? ?????????? ????????init();?? ????????if(list_msg==null){?? ????????????getData();?? ????????}?? ?????????????? ????????adapter=new?ArticleAdapter(this,list_msg);?? ????????//設置監聽器?? ????????mylistview.setOnRershListener(new?OnRershListener());?? ????????mylistview.setAdapter(adapter);?? ????}?? ?????????? ????private?void?getData()?{?? ????????//?TODO?Auto-generated?method?stub?? ????????//獲得xml的資源?? ????????String?names[];?? ????????String?article[];?? ????????TypedArray?img;?? ????????int?i;???????? ????????names=getResources().getStringArray(R.array.name);?? ????????article=getResources().getStringArray(R.array.article);?? ????????img=getResources().obtainTypedArray(R.array.head_photo);?? ?????????? ????????list_msg=new?ArrayList<Messages>();?? ????????for(i=0;i<names.length;i++){?? ????????????Messages?message=new?Messages();?? ????????????message.setImg(img.getDrawable(i));?? ????????????message.setName(names[i]);?? ????????????message.setArticel(article[i]);?? ????????????message.setRq(String.valueOf(new?Random().nextInt(120)+"分鐘前"));?? ????????????Date?date=new?Date();?? ????????????SimpleDateFormat?simple=new?SimpleDateFormat("MM-dd");?? ????????????message.setTime(simple.format(date));?? ????????????list_msg.add(message);?? ????????}?? ????}?? ????private?void?init()?{?? ????????//?TODO?Auto-generated?method?stub?? ????????mylistview=(MyListView)findViewById(R.id.myListView1);?? ????}?? ?? ????@Override?? ????public?boolean?onCreateOptionsMenu(Menu?menu)?{?? ?????? ????????getMenuInflater().inflate(R.menu.main,?menu);?? ????????return?true;?? ????}?? ?????? ?? ????Handler?handler=new?Handler(new?Handler.Callback()?{?? ????????@Override?? ????????public?boolean?handleMessage(Message?arg0)?{?? ????????????//?TODO?Auto-generated?method?stub?? ????????????if(arg0.what==0){????????????????? ????????????????//結束刷新?? ????????????????mylistview.endOnRersh();?? ????????????}?? ????????????return?false;?? ????????}?? ????});?? ?????? ????//刷新接口實現類?? ????class?OnRershListener?implements?OnRersh{?? ????????//實現刷新接口的?方法,,在MyList中會回調該方法。?? ????????//模擬?? ????????@Override?? ????????public?void?OnRershListener()?{?? ????????????//?TODO?Auto-generated?method?stub?? ????????????new?Thread(new?Run()).start();//啟動一個線程?? ????????}?? ????}?? ?? ????//線程接口實現類?? ????class?Run?implements?Runnable{?? ????????@Override?? ????????public?void?run()?{?? ????????????//?TODO?Auto-generated?method?stub?? ????????????try?{?? ????????????????Thread.sleep(2000);?? ????????????}?catch?(Exception?e)?{?? ????????????????//?TODO:?handle?exception?? ????????????????e.printStackTrace();?? ????????????}?? ????????????handler.sendEmptyMessage(0);?? ????????}?? ????}?? ?? }?? ?? </span>??
<span?style="font-family:Comic?Sans?MS;font-size:12px;">package?com.bzu.edu.gxs.adapter;?? ?? import?java.util.List;?? import?com.bzu.gxs.*;?? import?android.content.Context;?? import?android.view.LayoutInflater;?? import?android.view.View;?? import?android.view.ViewGroup;?? import?android.widget.BaseAdapter;?? import?android.widget.ImageView;?? import?android.widget.LinearLayout;?? import?android.widget.TextView;?? ?? public?class?ArticleAdapter?extends?BaseAdapter?{?? ????private?List<Messages>?list_mes=null;?? ????private?Context?context;?? ?????? ????public?ArticleAdapter(Context?context,List<Messages>?list_mes)?{?? ????????//?TODO?Auto-generated?constructor?stub?? ????????this.list_mes=list_mes;?? ????????this.context=context;?? ????}?? ?? ????@Override?? ????public?int?getCount()?{?? ????????//?TODO?Auto-generated?method?stub?? ????????return?list_mes.size();?? ????}?? ?? ????@Override?? ????public?Object?getItem(int?position)?{?? ????????//?TODO?Auto-generated?method?stub?? ????????return?list_mes.get(position);?? ????}?? ?? ????@Override?? ????public?long?getItemId(int?position)?{?? ????????//?TODO?Auto-generated?method?stub?? ????????return?position;?? ????}?? ?? ????@Override?? ????public?View?getView(int?position,?View?convertView,?ViewGroup?parent)?{?? ????????//?TODO?Auto-generated?method?stub?? ????????ListItem?listitem;?? ????????if(convertView==null)?? ????????{?? ????????????convertView=(LinearLayout)LayoutInflater.from(context).inflate(R.layout.layout_view,?null);?? ?????????????? ????????????listitem=new?ListItem();?? ????????????listitem.img=(ImageView)convertView.findViewById(R.id.head_img);?? ????????????listitem.name=(TextView)convertView.findViewById(R.id.name);?? ????????????//顯示當前日期?? ????????????listitem.time=(TextView)convertView.findViewById(R.id.time);?? ????????????listitem.rq=(TextView)convertView.findViewById(R.id.rq);?? ????????????listitem.article=(TextView)convertView.findViewById(R.id.article);?? ????????????convertView.setTag(listitem);?? ????????}else?{?? ????????????listitem=(ListItem)convertView.getTag();?? ????????}?? ?????????? ????????listitem.img.setImageDrawable(list_mes.get(position).getImg());?? ????????listitem.name.setText(list_mes.get(position).getName());?? ????????listitem.time.setText(list_mes.get(position).getTime());?? ????????listitem.rq.setText(list_mes.get(position).getRq());?? ????????listitem.article.setText(list_mes.get(position).getArticel());?? ????????return?convertView;?? ????}?? ?????? ????public?class?ListItem{?? ????????ImageView?img;?? ????????TextView?name;?? ????????TextView?time;?? ????????TextView?rq;?? ????????TextView?article;?? ????}?? }?? </span>??
<span?style="font-family:Comic?Sans?MS;font-size:12px;">package?com.bzu.edu.gxs.adapter;?? ?? import?android.graphics.drawable.Drawable;?? ?? public?class?Messages?{?? ????private?Drawable?img;?? ????private?String?name;?? ????private?String?time;?? ????private?String?rq;?? ????private?String?articel;?? ?????? ????public?Drawable?getImg()?{?? ????????return?img;?? ????}?? ????public?void?setImg(Drawable?img)?{?? ????????this.img?=?img;?? ????}?? ????public?String?getName()?{?? ????????return?name;?? ????}?? ????public?void?setName(String?name)?{?? ????????this.name?=?name;?? ????}?? ????public?String?getTime()?{?? ????????return?time;?? ????}?? ????public?void?setTime(String?time)?{?? ????????this.time?=?time;?? ????}?? ????public?String?getRq()?{?? ????????return?rq;?? ????}?? ????public?void?setRq(String?rq)?{?? ????????this.rq?=?rq;?? ????}?? ????public?String?getArticel()?{?? ????????return?articel;?? ????}?? ????public?void?setArticel(String?articel)?{?? ????????this.articel?=?articel;?? ????}?? ?????? ?? }</span><span?style="font-family:Comic?Sans?MS;">?? </span>?
通過ListView的學習,模擬了新浪微博的隨便看看頁面。
要實現的效果:
代碼如下:
[html] view plaincopyprint?????????? layout_view.xml
?????????
??? 代碼如下:
[html] view plaincopyprint?5.java代碼
???????? MainActivity.java
[html] view plaincopyprint???????????? ArticleAdapter.java
[html] view plaincopyprint?????????? Messages.java
[html] view plaincopyprint?總結
以上是生活随笔為你收集整理的android学习:模拟微博看看界面的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MATLAB二元隐函数绘图命令fimpl
- 下一篇: linux_设备驱动_设备树