zoukankan      html  css  js  c++  java
  • Android基于IIS的APK下载(一)自定义更新控件

    Android越来越普及,那已经安装的应用要如何更新呢?在应用市场中常会有显示某某应用已经更新之类的信息,那我们是否也可以实现类似的功能呢?如果要实现又要做哪些准备呢?做过WEB开发的,一般都会知道对于WEB服务器有很多,比如Windows自带的IIS、跨平台的Apache等,这里以IIS作为服务器实现APK的更新下载,下面是效果图。


    我们可以看到,在获取更新列表后,可以根据情况进行相应的更新,在下完更新后,进行安装,这样就完成了更新。

    在更新列表中显示的每一行都很相似,只是内容上有些变化,这种情况很容易联想到封装,那么我们又要如何封装出这样的控件呢?

    Android中的UI是在Layout中以xml来描述的,这和wpf有些相似。那我们是否可以将更新列表的每一行抽象成以xml形式描述的控件呢?答案是可以的!下面是相应的代码


    update_item.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:layout_width="fill_parent"
        android:layout_height="64px"
        android:paddingLeft="10px"
        android:paddingTop="10px"
        android:orientation="horizontal" >
    
        <ImageView
            android:id="@+id/update_item_app_image"
            android:layout_width="64px"
            android:layout_height="64px"
            android:background="@drawable/apk" >
        </ImageView>
    
        <TextView
            android:id="@+id/update_item_app_name"
          	style="@style/update_item_textview"
            android:text="TestApk" >
        </TextView>
    
        <TextView
            android:id="@+id/update_item_app_old_version"
           	style="@style/update_item_textview"
           	android:gravity="right"
            android:text="Not Install" >
        </TextView>
        
        <TextView      
           	style="@style/update_item_textview"
           	android:layout_width="50px"
           	android:gravity="center"
            android:text="->" >
        </TextView>
        
        <TextView
            android:id="@+id/update_item_new_version"
           	style="@style/update_item_textview"
           	android:gravity="left"
            android:text="1.0.0.0" >
        </TextView>
    
    
        <Button
            android:id="@+id/update_item_behavior"
            style="@style/update_item_button"
            android:text="Update" >
        </Button>
    
    </LinearLayout>
    注:

    1、update_item_app_image使用了一个名字为apk的背景图,可以更换。

    2、借助HTML+CSS的思想,将布局与样式渲染尽可能的解耦,TextView中使用style引用相应的样式,具体样式在后面的代码中。


    update_item_style.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources xmlns:android="http://schemas.android.com/apk/res/android">      
        <style name="update_item_textview">
            <item name="android:layout_width">180px</item>
            <item name="android:layout_height">wrap_content</item>
            <item name="android:layout_marginLeft">20px</item>
            <item name="android:textColor">#000000</item>
            <item name="android:textSize">30sp</item>
        </style>
        <style name="update_item_button">
            <item name="android:layout_width">180px</item>
            <item name="android:layout_height">wrap_content</item>
            <item name="android:layout_marginLeft">20px</item>
            <item name="android:textColor">#000000</item>
            <item name="android:textSize">30sp</item>
        </style>
    </resources>

    注:

    1、该xml文件放在values中,与update_item.xml相对应。

    2、为了便于查阅与维护,命名采用“功能+作用”的形式来命名,并且均为小写,不同单词间以下划线连接。比如style中是对应update_item的style,所以命名为update_item_style.xml,布局文件默认为功能名,即update_item.xml。再如布局文件的ID和style中的name也采用相似的命名法。

    3、对布局文件中的字串,最好也有一个对应的文件,比如update_item_strings.xml,这里仅作示例,没有给出。

    自定义好更新控件后,现在就是使用了。使用时,即要先找到这个布局,然后再添加到相应的位置中去。从效果图可以看到,更新列表是按行分布的,这个可以采用多种方式来实现,比如RelativeLayout+LinearLayout、ListView+Adapater等,这里采用ListView+Adapter来实现。

    具体请参看Android基于IIS的APK下载(二)显示更新列表

    转载请注明出处Android基于IIS的APK下载(一)自定义更新控件

    完整代码在此处下载https://github.com/sparkleDai/ApkUpdate

  • 相关阅读:
    fzuoj Problem 2177 ytaaa
    zoj The 12th Zhejiang Provincial Collegiate Programming Contest Capture the Flag
    zoj The 12th Zhejiang Provincial Collegiate Programming Contest Team Formation
    zoj The 12th Zhejiang Provincial Collegiate Programming Contest Beauty of Array
    zoj The 12th Zhejiang Provincial Collegiate Programming Contest Lunch Time
    zoj The 12th Zhejiang Provincial Collegiate Programming Contest Convert QWERTY to Dvorak
    zoj The 12th Zhejiang Provincial Collegiate Programming Contest May Day Holiday
    zoj The 12th Zhejiang Provincial Collegiate Programming Contest Demacia of the Ancients
    zjuoj The 12th Zhejiang Provincial Collegiate Programming Contest Ace of Aces
    csuoj 1335: 高桥和低桥
  • 原文地址:https://www.cnblogs.com/sparkleDai/p/7605040.html
Copyright © 2011-2022 走看看