Android 軟體管理器的開發1

來源:互聯網
上載者:User

一、布局

分三塊:1. 

                   2. 

 

 

      3. 

 

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

第二部分是列表(ListVIew)

第三部分是三個按鈕

 

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

 

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

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:umadsdk="http://schemas.android.com/apk/res/stane.appExplorer"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:background="#313849"><!--  1. 單位有dip、px,具體區別自己百度。       2. 字型的單位用 sp  3. gravity 是自己相對與父控制項的位置;Layout_gravity是自己的子控制項相對與自己的位置  4. orientation:   Layout中的控制項是水平 排列還是豎直  其它的可以參看文檔,文檔中寫的很詳細,也很簡單--><LinearLayout      android:layout_height="30dip"      android:layout_width="fill_parent"    android:orientation="horizontal"      android:gravity="center_vertical"    android:paddingLeft="5dip"    android:background="@drawable/top_bg">        <ImageView        android:layout_width="18dip"       android:layout_height="18dip"      android:src="@drawable/manage"/>  <TextView   android:layout_width="wrap_content"       android:layout_height="wrap_content"      android:textColor="#000"      android:textSize="14sp"      android:text="@string/app_type"/>    </LinearLayout>      <LinearLayout       android:layout_width="fill_parent"    android:layout_height="fill_parent"  android:layout_marginTop="28dip"    android:layout_marginBottom="100dip"    android:layout_marginLeft="5dip"    android:layout_marginRight="5dip">        <ListView    android:id="@+id/lv_apps"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:listSelector="@drawable/choose_listview"    android:visibility="gone"/>    </LinearLayout>                     <RelativeLayout     android:layout_width="fill_parent"     android:layout_height="50dip"     android:layout_alignParentBottom="true"     android:background="@drawable/bottom_bg">     <ImageButton     android:id="@+id/ib_change_view"     android:layout_alignParentLeft="true"     android:layout_width="70dip"     android:layout_height="45dip"     android:layout_marginRight="5dip"     android:layout_marginTop="3dip"     android:src="@drawable/list"/>     <ImageButton     android:id="@+id/ib_change_category"     android:layout_alignParentRight="true"     android:layout_width="70dip"     android:layout_height="45dip"     android:layout_marginRight="5dip"     android:layout_marginTop="3dip"     android:src="@drawable/all"/>     <Button     android:id="@+id/ib_shop"     android:layout_centerInParent="true"     android:layout_width="70dip"     android:layout_height="50dip"     android:text="淘軟體"     android:background="@drawable/button_install_selector"/>     </RelativeLayout>         </RelativeLayout>

二、 ListView

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

package com.stone.app;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.TextView;/** * 有四個方法要重寫 */public class ListViewAdapter extends BaseAdapter {LayoutInflater inflater;Context context;public ListViewAdapter(Context context) {// TODO Auto-generated constructor stubthis.context = context;inflater = LayoutInflater.from(context);}/** * getCount()中的傳回值決定了ListView有的列數 */@Overridepublic int getCount() {// TODO Auto-generated method stubreturn 10;}/** * 可以返回null, 也可以返回其它的資料。 */@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn null;}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn 0;}/** * 這個是最重要的方法。 傳回值就是列表中第position列要顯示的內容 * position: 當前的View位於第幾列 * convertView: 當前的的列上 要顯示的內容 */@Overridepublic View getView(int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stubView view = inflater.inflate(R.layout.lv_item, null);// lv_item.xml    就是每一列要顯示的內容ImageView imageView = (ImageView) view.findViewById(R.id.lv_icon);    TextView tv_appname = (TextView) view.findViewById(R.id.lv_item_appname);TextView tv_package = (TextView) view.findViewById(R.id.lv_item_packagename);imageView.setBackgroundResource(R.drawable.icon);tv_appname.setText("應用的名稱");tv_package.setText(context.getResources().getString(R.string.packageName));return view;}}

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

@Overridepublic View getView(int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stubHolder view = null;if (convertView == null) {view = new Holder();convertView = inflater.inflate(R.layout.lv_item, null);view.imageView = (ImageView) convertView.findViewById(R.id.lv_icon);view.tv_appname = (TextView) convertView.findViewById(R.id.lv_item_appname);view.tv_package = (TextView) convertView.findViewById(R.id.lv_item_packagename);convertView.setTag(view);//通過setTag把該view儲存起來。} else {view = (Holder) convertView.getTag();  //convertView不為空白是,直接拿到儲存的view}view.imageView.setBackgroundResource(R.drawable.icon);view.tv_appname.setText("應用的名稱");view.tv_package.setText(context.getResources().getString(R.string.packageName));return convertView;}

Holder是一個內部類:

class Holder {ImageView imageView;TextView tv_appname;TextView tv_package;TextView textView2;ImageView imageView2;}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.