歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android 軟件管理器的開發

Android 軟件管理器的開發

日期:2017/3/1 11:15:03   编辑:Linux編程


一、布局

分三塊:

1.

2.

3.

第一部分是一張圖(ImageView) 和 幾個字(TextView)

第二部分是列表(ListVIew)

第三部分是三個按鈕

具體怎麼去放到合理的位置就不具體說了。自己慢慢試,這樣才能熟練。(提示: 可以用相對布局àRelativeLayout 來整體布局這三塊)。

可以參考項目中的show.xml:已經寫好了注釋

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:Android="http://schemas.android.com/apk/res/android"
  3. xmlns:umadsdk="http://schemas.android.com/apk/res/stane.appExplorer"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. android:background="#313849">
  7. <!-- 1. 單位有dip、px,具體區別自己百度。
  8. 2. 字體的單位用 sp
  9. 3. gravity 是自己相對與父控件的位置;Layout_gravity是自己的子控件相對與自己的位置
  10. 4. orientation: Layout中的控件是水平 排列還是豎直
  11. 其它的可以參看文檔,文檔中寫的很詳細,也很簡單-->
  12. <LinearLayout
  13. android:layout_height="30dip"
  14. android:layout_width="fill_parent"
  15. android:orientation="horizontal"
  16. android:gravity="center_vertical"
  17. android:paddingLeft="5dip"
  18. android:background="@drawable/top_bg">
  19. <ImageView
  20. android:layout_width="18dip"
  21. android:layout_height="18dip"
  22. android:src="@drawable/manage"/>
  23. <TextView
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content"
  26. android:textColor="#000"
  27. android:textSize="14sp"
  28. android:text="@string/app_type"/>
  29. </LinearLayout>
  30. <LinearLayout
  31. android:layout_width="fill_parent"
  32. android:layout_height="fill_parent"
  33. android:layout_marginTop="28dip"
  34. android:layout_marginBottom="100dip"
  35. android:layout_marginLeft="5dip"
  36. android:layout_marginRight="5dip">
  37. <ListView
  38. android:id="@+id/lv_apps"
  39. android:layout_width="fill_parent"
  40. android:layout_height="fill_parent"
  41. android:listSelector="@drawable/choose_listview"
  42. android:visibility="gone"/>
  43. </LinearLayout>
  44. <RelativeLayout
  45. android:layout_width="fill_parent"
  46. android:layout_height="50dip"
  47. android:layout_alignParentBottom="true"
  48. android:background="@drawable/bottom_bg">
  49. <ImageButton
  50. android:id="@+id/ib_change_view"
  51. android:layout_alignParentLeft="true"
  52. android:layout_width="70dip"
  53. android:layout_height="45dip"
  54. android:layout_marginRight="5dip"
  55. android:layout_marginTop="3dip"
  56. android:src="@drawable/list"/>
  57. <ImageButton
  58. android:id="@+id/ib_change_category"
  59. android:layout_alignParentRight="true"
  60. android:layout_width="70dip"
  61. android:layout_height="45dip"
  62. android:layout_marginRight="5dip"
  63. android:layout_marginTop="3dip"
  64. android:src="@drawable/all"/>
  65. <Button
  66. android:id="@+id/ib_shop"
  67. android:layout_centerInParent="true"
  68. android:layout_width="70dip"
  69. android:layout_height="50dip"
  70. android:text="淘軟件"
  71. android:background="@drawable/button_install_selector"/>
  72. </RelativeLayout>
  73. </RelativeLayout>
二、 ListView

關於ListView 要用 適配器來填充內容。 適配器有好幾種: SimpleAdapter、ArrayAdapter等。 不過項目中經常要自己繼承基類BaseAdapter 。

  1. package com.stone.app;
  2. import android.content.Context;
  3. import android.view.LayoutInflater;
  4. import android.view.View;
  5. import android.view.ViewGroup;
  6. import android.widget.BaseAdapter;
  7. import android.widget.ImageView;
  8. import android.widget.TextView;
  9. /**
  10. * 有四個方法要重寫
  11. */
  12. public class ListViewAdapter extends BaseAdapter {
  13. LayoutInflater inflater;
  14. Context context;
  15. public ListViewAdapter(Context context) {
  16. // TODO Auto-generated constructor stub
  17. this.context = context;
  18. inflater = LayoutInflater.from(context);
  19. }
  20. /**
  21. * getCount()中的返回值決定了ListView有的列數
  22. */
  23. @Override
  24. public int getCount() {
  25. // TODO Auto-generated method stub
  26. return 10;
  27. }
  28. /**
  29. * 可以返回null, 也可以返回其它的數據。
  30. */
  31. @Override
  32. public Object getItem(int position) {
  33. // TODO Auto-generated method stub
  34. return null;
  35. }
  36. @Override
  37. public long getItemId(int position) {
  38. // TODO Auto-generated method stub
  39. return 0;
  40. }
  41. /**
  42. * 這個是最重要的方法。 返回值就是列表中第position列要顯示的內容
  43. * position: 當前的View位於第幾列
  44. * convertView: 當前的的列上 要顯示的內容
  45. */
  46. @Override
  47. public View getView(int position, View convertView, ViewGroup parent) {
  48. // TODO Auto-generated method stub
  49. View view = inflater.inflate(R.layout.lv_item, null);// lv_item.xml 就是每一列要顯示的內容
  50. ImageView imageView = (ImageView) view.findViewById(R.id.lv_icon);
  51. TextView tv_appname = (TextView) view.findViewById(R.id.lv_item_appname);
  52. TextView tv_package = (TextView) view.findViewById(R.id.lv_item_packagename);
  53. imageView.setBackgroundResource(R.drawable.icon);
  54. tv_appname.setText("應用的名稱");
  55. tv_package.setText(context.getResources().getString(R.string.packageName));
  56. return view;
  57. }
  58. }

再來看上面的代碼, 在getView中,每列都會new 一個view,然後去填充相應的數據, 這就需要注意了,因為getView在每項顯示的時候就會調用(包括滾動重新顯示)就會調用。 這樣就會浪費手機的資源。其實每一項的view只需要填充一次。下面的getView優化後的代碼

  1. @Override
  2. public View getView(int position, View convertView, ViewGroup parent) {
  3. // TODO Auto-generated method stub
  4. Holder view = null;
  5. if (convertView == null) {
  6. view = new Holder();
  7. convertView = inflater.inflate(R.layout.lv_item, null);
  8. view.imageView = (ImageView) convertView
  9. .findViewById(R.id.lv_icon);
  10. view.tv_appname = (TextView) convertView
  11. .findViewById(R.id.lv_item_appname);
  12. view.tv_package = (TextView) convertView
  13. .findViewById(R.id.lv_item_packagename);
  14. convertView.setTag(view);//通過setTag把該view保存起來。
  15. } else {
  16. view = (Holder) convertView.getTag(); //convertView不為空是,直接拿到保存的view
  17. }
  18. view.imageView.setBackgroundResource(R.drawable.icon);
  19. view.tv_appname.setText("應用的名稱");
  20. view.tv_package.setText(context.getResources().getString(R.string.packageName));
  21. return convertView;
  22. }
Holder是一個內部類:
  1. class Holder {
  2. ImageView imageView;
  3. TextView tv_appname;
  4. TextView tv_package;
  5. TextView textView2;
  6. ImageView imageView2;
  7. }
Copyright © Linux教程網 All Rights Reserved