Android新闻客户端案列

来源:互联网 发布:淘宝仿真左轮 编辑:程序博客网 时间:2024/04/30 02:06

前言:这是一个仿照网易客户端模仿的简易新闻客户端案例,主要是为了对知识点更深入的了解和整合

1 需求

第一次进入新闻客户端需要请求服务器获取新闻数据,做listview的展示,为了第二次再次打开新闻客户端时能快速显示新闻,需要将数据缓存到数据库中,下次打开可以直接去数据库中获取新闻直接做展示。

  2 程序流程

 1.写布局listview 

 2.找到listview,设置条目的点击事件。 

 3.获取数据提供给listview做展示。       //☆
         
 4.创建一个Adapter继承BaseAdapter,封装4个方法,需要接收获取的新闻数据  

 5.将adapter设置给listview。  
3

     3  用到的知识点

1 Handler机制   2 ListView   3 BaseAdapter(适配器)   4 SqliteOpenHelper 数据库操作   5  自定义组件(MyImageView)

6 数据传输json  等

     4 服务器端

        用到的jar包 java-json.jar

[html] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:14px;">package com.itheima.service;  
  2.   
  3. import java.io.IOException;  
  4. import java.sql.*;  
  5. import java.util.ArrayList;  
  6.   
  7. import javax.servlet.ServletException;  
  8. import javax.servlet.http.HttpServlet;  
  9. import javax.servlet.http.HttpServletRequest;  
  10. import javax.servlet.http.HttpServletResponse;  
  11.   
  12. import org.json.JSONArray;  
  13. import org.json.JSONObject;  
  14.   
  15. import com.itheima.service.bean.NewsBean;  
  16. import com.itheima.service.dao.NewsDao;  
  17.   
  18.   
  19. public class GetNewsServlet extends HttpServlet {  
  20.   
  21.     public GetNewsServlet() {  
  22.         super();  
  23.     }  
  24.   
  25.     public void destroy() {  
  26.         super.destroy();   
  27.     }  
  28.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  29.     throws ServletException, IOException {  
  30.         /*  
  31.     新闻请求路径:  
  32. //  http://192.168.1.103:8080/itheima72/servlet/GetNewsServlet  
  33.       
  34.     json格式:  
  35.     用{}包含的是一个JsonObject  用[]包含的是一个JsonArray  
  36.   
  37.      */  
  38.   
  39.         try{  
  40.             //从数据库获取新闻数据  
  41.             ArrayList<NewsBean> news = NewsDao.getNews();  
  42.   
  43.             //将list中的数据封装成一个jsonArray对象  
  44.             JSONArray jsonArray = new JSONArray();  
  45.             for (NewsBean newsBean : news) {  
  46.                 JSONObject newsJson = new JSONObject();  
  47.                 newsJson.put("id", newsBean.getId());  
  48.                 newsJson.put("title", newsBean.getTitle());  
  49.                 newsJson.put("des", newsBean.getDes());  
  50.                 newsJson.put("icon_url", newsBean.getIcon_url());  
  51.                 newsJson.put("news_url", newsBean.getNews_url());  
  52.                 newsJson.put("type", newsBean.getType());  
  53.                 newsJson.put("time", newsBean.getTime());  
  54.                 newsJson.put("comment", newsBean.getComment());  
  55.                 jsonArray.put(newsJson);  
  56.   
  57.             }  
  58.   
  59.             //将jsonArray对象作为一个json对象,用来返回给客户端  
  60.             JSONObject allNewsJson = new JSONObject();  
  61.             allNewsJson.put("newss", jsonArray);  
  62.             response.getOutputStream().write(allNewsJson.toString().getBytes("gbk"));  
  63.   
  64.         }catch (Exception e) {  
  65.         }  
  66.     }  
  67.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  68.     throws ServletException, IOException {  
  69.   
  70.         doGet(request, response);  
  71.     }  
  72.   
  73.     public void init() throws ServletException {  
  74.           
  75.     }  
  76.   
  77. }</span>  

5 Android 客户端

5.1 强新闻数据封装成NewsBean

[html] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:14px;">public class NewsBean {  
  2.     public String title;  
  3.     public String des;  
  4.     public String news_url;  
  5.     public int id;  
  6.     public int comment;  
  7.     public int type;  
  8.     public String time;  
  9.     public String icon_url;  
  10. }</span>  

5.2 数据库的操作

[html] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:14px;">package com.example.dao;  
  2.   
  3. import android.content.Context;  
  4. import android.database.sqlite.SQLiteDatabase;  
  5. import android.database.sqlite.SQLiteOpenHelper;  
  6.   
  7. /**  
  8.  * Created by ls on 2016/9/24.  
  9.  */  
  10. public class NewsOpenHelper extends SQLiteOpenHelper {  
  11.   
  12.   
  13.     public NewsOpenHelper(Context context) {  
  14.         super(context, "news", null, 1);  
  15.     }  
  16.   
  17.     @Override  
  18.     public void onCreate(SQLiteDatabase db) {  
  19.         db.execSQL("create table news (_id integer primary key ,title varchar(200),des varchar(300),icon_url varchar(200),news_url varchar(200)," +  
  20.                 " type integer , time varchar(100),comment integer)");  
  21.     }  
  22.   
  23.     @Override  
  24.     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {  
  25.   
  26.     }  
  27. }</span>  
DAO 层
[html] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. <span style="font-family:SimSun;font-size:14px;">package com.example.dao;  
  2.   
  3. import android.content.ContentValues;  
  4. import android.content.Context;  
  5. import android.database.Cursor;  
  6. import android.database.sqlite.SQLiteDatabase;  
  7. import com.example.bean.NewsBean;  
  8. import java.util.ArrayList;  
  9. public class NewsDaoUtils {  
  10.     private NewsOpenHelper openHelper;  
  11.     public NewsDaoUtils(Context context) {  
  12.         //创建一个帮助类对象  
  13.         openHelper = new NewsOpenHelper(context);  
  14.     }  
  15.     //删除数据库中缓存的旧数据  
  16.     public void delete() {  
  17.         //通过帮助类对象获取数据库操作对象  
  18.         SQLiteDatabase database = openHelper.getReadableDatabase();  
  19.         database.delete("news", null, null);  
  20.         database.close();  
  21.     }  
  22.     //向数据库中添加新闻数据  
  23.     public void saveNews(ArrayList<NewsBean> list) {  
  24.         //通过帮助类对象获取数据库操作对象  
  25.         SQLiteDatabase db = openHelper.getReadableDatabase();  
  26.         for (NewsBean newsBean:list) {  
  27.             ContentValues values = new ContentValues();  
  28.             values.put("_id", newsBean.id);  
  29.             values.put("title", newsBean.title);  
  30.             values.put("des", newsBean.des);  
  31.             values.put("icon_url", newsBean.icon_url);  
  32.             values.put("news_url", newsBean.news_url);  
  33.             values.put("type", newsBean.type);  
  34.             values.put("time", newsBean.time);  
  35.             values.put("comment", newsBean.comment);  
  36.             db.insert("news", null, values);  
  37.         }  
  38.         db.close();  
  39.     }  
  40.   
  41.     //从数据库中获取缓存的新闻数据  
  42.     public  ArrayList<NewsBean> getNews() {  
  43.         ArrayList<NewsBean> list = new ArrayList<>();  
  44.         //通过帮助类对象获取数据库操作对象  
  45.         SQLiteDatabase db = openHelper.getReadableDatabase();  
  46.         Cursor cursor = db.rawQuery("select * from news ", null);  
  47.         while (cursor.moveToNext()) {  
  48.             NewsBean newsBean = new NewsBean();  
  49.             //1 根据列名获取列索引  
  50.             // newsBean. id = cursor.getInt(cursor.getColumnIndex("_id"));  
  51.   
  52.             //2 根据列的索引直接读取  比如第0列的值  
  53.             newsBean. id = cursor.getInt(0);  
  54.             newsBean. title = cursor.getString(1);  
  55.             newsBean. des = cursor.getString(2);  
  56.             newsBean. icon_url =    cursor.getString(3);  
  57.             newsBean. news_url =    cursor.getString(4);  
  58.             newsBean.   type = cursor.getInt(5);  
  59.             newsBean. time =    cursor.getString(6);  
  60.             newsBean.   comment = cursor.getInt(7);  
  61.   
  62.             list.add(newsBean);  
  63.         }  
  64.         db.close();  
  65.         cursor.close();  
  66.         return list;  
  67.     }  
  68.   
  69. }</span>  

5.3 适配器

[html] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:14px;">package com.example.adapter;  
  2.   
  3. import android.content.Context;  
  4. import android.view.LayoutInflater;  
  5. import android.view.View;  
  6. import android.view.ViewGroup;  
  7. import android.widget.BaseAdapter;  
  8. import android.widget.TextView;  
  9. import com.example.bean.NewsBean;  
  10. import com.example.newslist.R;  
  11. import com.example.view.MyImageView;  
  12. import java.util.ArrayList;  
  13. public class NewsAdapter extends BaseAdapter {  
  14.     Context context;  
  15.     ArrayList<NewsBean> list;  
  16.     //通过构造方法接收要显示的新闻类数据集合  
  17.     public NewsAdapter(Context context) {  
  18.         this.context =  context;  
  19.     }  
  20.     public NewsAdapter(Context context, ArrayList<NewsBean> list) {  
  21.         this.list =  list;  
  22.         this.context =  context;  
  23.     }  
  24.     @Override  
  25.     public Object getItem(int position) {  
  26.         return list.get(position);  
  27.     }  
  28.   
  29.     @Override  
  30.     public long getItemId(int position) {  
  31.         return position;  
  32.     }  
  33.   
  34.     @Override  
  35.     public int getCount() {  
  36.         return list.size();  
  37.     }  
  38.   
  39.     @Override  
  40.     public View getView(int position, View convertView, ViewGroup parent) {  
  41.         View view = null;  
  42.         //1 复用convertView 优化listView ,创建一个view作为getView的返回值,用来显示条目  
  43.         if (convertView != null) {  
  44.             view =convertView;  
  45.         }else{  
  46.             //context:上下文  resource:要转换成view对象的layout的id;  
  47.             //root:将layout用root(ViewGroup) 作为codify的返回值,一般传null  
  48.   
  49.             //通过LayoutInflater将布局转换成view对象  
  50.            // view = LayoutInflater.from(context).inflate(R.layout.item_news_layout, null);  
  51.             //通过context获取系统服务得到一个LayoutInflater,通过LayoutInflater将一个布局转换成view对象  
  52.             LayoutInflater inflater =  (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  53.             view = inflater.inflate(R.layout.item_news_layout,null);  
  54.         }  
  55.             //获取view上的子控件对象  
  56.             MyImageView myImageView = (MyImageView)view.findViewById(R.id.item_img_icon);  
  57.             TextView title = (TextView)view.findViewById(R.id.item_tv_title);  
  58.             TextView des = (TextView)view.findViewById(R.id.item_tv_des);  
  59.             TextView comment = (TextView)view.findViewById(R.id.item_tv_comment);  
  60.             TextView type = (TextView)view.findViewById(R.id.item_tv_type);  
  61.             //获取positon位置条目,对应的list集合中的新闻数据,Bean对象  
  62.             NewsBean newsBean = list.get(position);  
  63.             myImageView.setImageUrl(newsBean.icon_url);  
  64.             title.setText(newsBean.title);  
  65.             des.setText(newsBean.des);  
  66.             comment.setText("评论 :"+newsBean.comment);  
  67.       
  68.             //0头条 1娱乐 2 新闻  
  69.             switch (newsBean.type) {  
  70.                 case 0:  
  71.                     type.setText("头条");  
  72.                     break;  
  73.                 case 1:  
  74.                     type.setText("娱乐");  
  75.                     break;  
  76.                 case 2:  
  77.                     type.setText("新闻");  
  78.                     break;  
  79.                 default:  
  80.                     break;  
  81.             }  
  82.         return view;  
  83.     }  
  84. }</span>  

5.4 请求服务端数据,并封装成List<NewsBean>返回 这一步也是最关键的

[html] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:14px;">package com.example.utils;  
  2. import android.content.Context;  
  3. import com.example.bean.NewsBean;  
  4. import com.example.dao.NewsDaoUtils;  
  5. import org.json.JSONArray;  
  6. import org.json.JSONObject;  
  7. import java.io.InputStream;  
  8. import java.net.HttpURLConnection;  
  9. import java.net.URL;  
  10. import java.util.ArrayList;  
  11. public class NewsUtils {  
  12.     public static String newsPath_url = "http://192.168.1.106:8080/itheima74/servlet/GetNewsServlet";  
  13.   
  14.     //封装新闻的假数据到list中返回  
  15.     public static ArrayList<NewsBean> getAllNewsForNetWork(Context context) {  
  16.         ArrayList<NewsBean> list = new ArrayList<>();  
  17.         try {  
  18.             //请求服务器获取新闻数据  
  19.             URL url = new URL(newsPath_url);  
  20.             HttpURLConnection connection = (HttpURLConnection) url.openConnection();  
  21.             //设置连接的方式和超时  
  22.             connection.setRequestMethod("GET");  
  23.             connection.setConnectTimeout(2000);  
  24.             //获取请求的响应码  
  25.             int code = connection.getResponseCode();  
  26.   
  27.             if (code == 200) {  
  28.                 //获取请求到的流信息  
  29.                 InputStream is = connection.getInputStream();  
  30.                 String result = StreamUtils.stream2String(is);  
  31.                 //2解析获取的新闻数据到list集合中  
  32.                 JSONObject jsonObject = new JSONObject(result);//将一个字符串封装成一个json对象  
  33.                 JSONArray jsonArray = jsonObject.getJSONArray("newss");//获取json_root中newss作为jsonArray对象  
  34.                 for (int i = 0; i < jsonArray.length(); i++) {//循环遍历新闻array  
  35.                     JSONObject news_json = jsonArray.getJSONObject(i);//获取一条新闻的json  
  36.                     NewsBean newsBean = new NewsBean();  
  37.                     newsBean.id = news_json.getInt("id");  
  38.                     newsBean.comment = news_json.getInt("comment");//评论数  
  39.                     newsBean.type = news_json.getInt("type");//新闻的类型,0 :头条 1 :娱乐 2.体育  
  40.                     newsBean.time = news_json.getString("time");  
  41.                     newsBean.des = news_json.getString("des");  
  42.                     newsBean.title = news_json.getString("title");  
  43.                     newsBean.news_url = news_json.getString("news_url");  
  44.                     newsBean.icon_url = news_json.getString("icon_url");  
  45.   
  46.                     list.add(newsBean); //封装成对象添加到list集合中  
  47.                 }  
  48.                 //清楚数据库中的旧的数据,把新的数据缓存到数据库中  
  49.                 new NewsDaoUtils(context).delete();  
  50.                 new NewsDaoUtils(context).saveNews(list);  
  51.             }  
  52.         } catch (Exception e) {  
  53.             e.printStackTrace();  
  54.         }  
  55.         return list;  
  56.     }  
  57.     //从数据库中获取上次缓存的数据  
  58.     public static ArrayList<NewsBean> getAllNewsFromDB(Context context) {  
  59.         return  new NewsDaoUtils(context).getNews();  
  60.     }  
  61. }</span>  
中间用到了一个工具类 流转字符串
[html] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:14px;">package com.example.utils;  
  2.   
  3. import java.io.ByteArrayOutputStream;  
  4. import java.io.IOException;  
  5. import java.io.InputStream;  
  6.   
  7. /**  
  8.  * Created by ls on 2016/9/24.  
  9.  */  
  10. public class StreamUtils {  
  11.     public static String stream2String(InputStream is) {  
  12.   
  13.         //返回一个字节数组写入流  
  14.         ByteArrayOutputStream bos = new ByteArrayOutputStream();  
  15.         byte[] buffer = new byte[1024];  
  16.         int size = 0;  
  17.         String retValue = "";  
  18.         try {  
  19.             while ((size = is.read(buffer)) != -1) {  
  20.                 bos.write(buffer, 0, size);  
  21.             }  
  22.             retValue = new String(bos.toByteArray(), "gbk");  
  23.         } catch (IOException e) {  
  24.             e.printStackTrace();  
  25.         }  
  26.         return retValue;  
  27.     }  
  28. }</span>  

5.5 自定义处理图片的组件

[html] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:14px;">package com.example.view;  
  2. import android.content.Context;  
  3. import android.graphics.Bitmap;  
  4. import android.graphics.BitmapFactory;  
  5. import android.os.Handler;  
  6. import android.os.Message;  
  7. import android.util.AttributeSet;  
  8. import android.widget.ImageView;  
  9. import java.io.InputStream;  
  10. import java.net.HttpURLConnection;  
  11. import java.net.URL;  
  12. /**  
  13.  * Created by ls on 2016/9/24.  
  14.  * 自定义显示图片的组件  
  15.  */  
  16. public class MyImageView extends ImageView {  
  17.   
  18.     public MyImageView(Context context) {  
  19.         super(context);  
  20.     }  
  21.   
  22.     public MyImageView(Context context, AttributeSet attrs) {  
  23.         super(context, attrs);  
  24.     }  
  25.   
  26.     public MyImageView(Context context, AttributeSet attrs, int defStyleAttr) {  
  27.         super(context, attrs, defStyleAttr);  
  28.     }  
  29.   
  30.     private Handler handler = new Handler(){  
  31.         @Override  
  32.         public void handleMessage(Message msg) {  
  33.             Bitmap bitmap =(Bitmap) msg.obj;  
  34.             MyImageView.this.setImageBitmap(bitmap);  
  35.         }  
  36.     };  
  37.     public  void setImageUrl(final String url_str) {  
  38.         new Thread(new Runnable() {  
  39.             @Override  
  40.             public void run() {  
  41.                 try {  
  42.                     URL url = new URL(url_str);  
  43.                     HttpURLConnection connection = (HttpURLConnection)url.openConnection();  
  44.                     connection.setRequestMethod("GET");  
  45.                     connection.setConnectTimeout(2000);  
  46.                     int code = connection.getResponseCode();  
  47.                     if (code == 200) {  
  48.                         InputStream is=connection.getInputStream();  
  49.                         Bitmap bitmap = BitmapFactory.decodeStream(is);  
  50.                         Message message = Message.obtain();  
  51.                         message.obj = bitmap;  
  52.                         handler.sendMessage(message);  
  53.                     }  
  54.                 } catch (Exception e) {  
  55.                     e.printStackTrace();  
  56.                 }  
  57.             }  
  58.         }).start();  
  59.     }  
  60. }</span>  

5.6 MainActivity

[html] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. p<span style="font-size:14px;">ackage com.example.newslist;  
  2. import android.app.Activity;  
  3. import android.content.Context;  
  4. import android.content.Intent;  
  5. import android.net.Uri;  
  6. import android.os.Bundle;  
  7. import android.os.Handler;  
  8. import android.os.Message;  
  9. import android.view.View;  
  10. import android.widget.AdapterView;  
  11. import android.widget.ListView;  
  12. import com.example.adapter.NewsAdapter;  
  13. import com.example.bean.NewsBean;  
  14. import com.example.utils.NewsUtils;  
  15.   
  16. import java.util.ArrayList;  
  17. public class MainActivity extends Activity implements AdapterView.OnItemClickListener{  
  18.     private Context mcontext;  
  19.     private ListView listView;  
  20.     private Handler handler = new Handler(){  
  21.         @Override  
  22.         public void handleMessage(Message msg) {  
  23.   
  24.             ArrayList<NewsBean> allnews = (ArrayList<NewsBean>)msg.obj;  
  25.             if (allnews != null && allnews.size() > 0) {  
  26.                 for (NewsBean newsBean: allnews) {  
  27.                     NewsAdapter adapter = new NewsAdapter(mcontext,allnews);  
  28.                     listView.setAdapter(adapter);  
  29.                 }  
  30.             }  
  31.         }  
  32.     };  
  33.   
  34.     @Override  
  35.     protected void onCreate(Bundle savedInstanceState) {  
  36.         super.onCreate(savedInstanceState);  
  37.         mcontext = this;  
  38.         setContentView(R.layout.activity_main);  
  39.         listView = (ListView) findViewById(R.id.listview);  
  40.         //1先去数据库中去获取缓存的新闻数据,显示到新闻列表中  
  41.         ArrayList<NewsBean> list =  NewsUtils.getAllNewsFromDB(mcontext);  
  42.         if (list != null && list.size() >0) {  
  43.             //创建一个adapter设置给listview  
  44.             NewsAdapter adapter = new NewsAdapter(mcontext,list);  
  45.             listView.setAdapter(adapter);  
  46.         }  
  47.         //2通过网络去获取服务器上的新闻数据,用list封装,获取网络数据需要在子线程中去做  
  48.         new Thread(new Runnable() {  
  49.             @Override  
  50.             public void run() {  
  51.                 //请求网络数据通过  
  52.                 ArrayList<NewsBean> allnews = NewsUtils.getAllNewsForNetWork(mcontext);  
  53.                 //通过handler,将数据发送到主线程中去更新  
  54.                 Message message = Message.obtain();  
  55.                  message.obj=allnews;  
  56.                 handler.sendMessage(message);  
  57.             }  
  58.         }).start();  
  59.         //设置listView条目的点击事件  
  60.         listView.setOnItemClickListener(this);  
  61.     }  
  62.     //listview 条目点击时会调用这些方法,parent 代表listview ;view 点击条目上的那个view对象  
  63.     // positon 条目的位置 ; id 条目的id  
  64.     @Override  
  65.     public void onItemClick(AdapterView<?> parent, View view, int position, long id) {  
  66.         //需要获取条目上的bean对象中url做跳转  
  67.        NewsBean  newsBean = (NewsBean) parent.getItemAtPosition(position);  
  68.         String url = newsBean.news_url;  
  69.         //跳转到浏览器  
  70.         Intent intent = new Intent();  
  71.         intent.setAction(Intent.ACTION_VIEW);  
  72.         intent.setData(Uri.parse(url));  
  73.         startActivity(intent);  
  74.     }  
  75. }</span>  

6 Layout布局

activity_main.xml

[html] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical">  
  6.     <ListView  
  7.         android:id="@+id/listview"  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="match_parent"  
  10.          />  
  11. </LinearLayout></span>  

行布局 item_news_layout.xml
[html] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:paddingBottom="20dp"  
  4.     android:paddingTop="20dp"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     android:orientation="horizontal">  
  8.   
  9.     <com.example.view.MyImageView  
  10.         android:id="@+id/item_img_icon"  
  11.         android:layout_width="60dp"  
  12.         android:background="#ff0000"  
  13.         android:layout_height="60dp"  
  14.         android:scaleType="fitXY"  
  15.         android:layout_gravity="center"  
  16.         android:src="@drawable/ic_launcher"  
  17.         android:layout_marginRight="10dp"  
  18.   
  19.         />  
  20.     <LinearLayout  
  21.         android:layout_width="match_parent"  
  22.         android:layout_height="wrap_content"  
  23.         android:layout_gravity="center"  
  24.         android:orientation="vertical"  
  25.         >  
  26.   
  27.         <TextView  
  28.         android:id="@+id/item_tv_title"  
  29.         android:layout_width="match_parent"  
  30.         android:layout_height="wrap_content"  
  31.         android:layout_marginBottom="3dp"  
  32.         android:singleLine="true"  
  33.         android:text="title"  
  34.         android:textColor="#000000"  
  35.         android:textSize="20sp"  
  36.         />  
  37.   
  38.         <TextView  
  39.             android:id="@+id/item_tv_des"  
  40.             android:layout_width="match_parent"  
  41.             android:layout_height="wrap_content"  
  42.             android:layout_marginBottom="3dp"  
  43.             android:maxLines="2"  
  44.             android:text="des"  
  45.             android:textColor="#666666"  
  46.             android:textSize="16sp"  
  47.             />  
  48.         <RelativeLayout  
  49.             android:layout_width="match_parent"  
  50.             android:layout_height="wrap_content"  
  51.             android:layout_gravity="center"  
  52.             >  
  53.         <TextView  
  54.             android:id="@+id/item_tv_comment"  
  55.             android:layout_width="wrap_content"  
  56.             android:layout_height="wrap_content"  
  57.             android:layout_alignParentLeft="true"  
  58.             android:text="comment"  
  59.             android:textColor="#666666"  
  60.             android:textSize="16sp"  
  61.             />  
  62.             <TextView  
  63.                 android:id="@+id/item_tv_type"  
  64.                 android:layout_width="wrap_content"  
  65.                 android:layout_height="wrap_content"  
  66.                 android:layout_alignParentRight="true"  
  67.                 android:text="娱乐"  
  68.                 android:textColor="#666666"  
  69.                 android:textSize="16sp"  
  70.                 android:layout_marginRight="10dp"  
  71.                 />  
  72.   
  73.   
  74.         </RelativeLayout>  
  75.   
  76.     </LinearLayout>  
  77. </LinearLayout>  

7 权限

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

0 1
原创粉丝点击