0%

拖动控件原理其实很简单,重写activity的onTouchEvent方法,根据手指所在位置得到x,y座标,再用AbsoluteLayout把指定的控件设置到该位置。 首先,必须用AbsoluteLayout绝对布局:

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
<TextView  android:id="@+id/tv"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
</AbsoluteLayout>

JAVA代码:

package com.pocketdigi.move;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;
import android.widget.AbsoluteLayout.LayoutParams;

@SuppressWarnings("deprecation")
public class Main extends Activity {
    /** Called when the activity is first created. */
    TextView tv;
    boolean flag = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv = (TextView) findViewById(R.id.tv);
        tv.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                flag = true;
                // 当控件被点中时,flag设为true
                //不能写在onClick事件中
                return false;
            }

        });

    }

    @SuppressWarnings("deprecation")
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        if (flag) {
            // flag为true即控件被点到时,执行移动控件操作
            int x = (int) event.getX();
            int y = (int) event.getY();
            // 得到X,Y座标
            LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT, x - 10, y - 40);
            // 四参数分别为宽,高,X,Y座标,wrap_conent为根据内容自动调整
            // 后面-10,-40是我自己多次调试的结果,因为我发现如果不减,那个座标并不是在指头下,而是在指头的右下角
            // 暂时不知道什么原因
            tv.setLayoutParams(params);
            // 设置最终位置

        }
        if (event.getAction() == MotionEvent.ACTION_UP) {
            // 手指离开屏幕时,把flag设为false
            flag = false;
        }

        return super.onTouchEvent(event);

    }
}

如果屏幕上有多个控件需要移动,可以加个int,用于存储控件的ID 另一种方法:

int[] temp = new int[] { 0, 0 };
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub

        int eventaction = event.getAction();

        int x = (int) event.getRawX();
        int y = (int) event.getRawY();
        int p = (int) event.getX();
        int q = (int) event.getY();


        switch (eventaction) {

        case MotionEvent.ACTION_DOWN: 
            temp[0] = (int) event.getX();
            temp[1] = y - v.getTop();
            break;
        case MotionEvent.ACTION_MOVE: 
            int l = x - temp[0];
            int t = y - temp[1];
            int r = x + v.getWidth() - temp[0];
            int b = y - temp[1] + v.getHeight();
            
            v.layout(l, t, r,b);
            v.postInvalidate();
            break;

        case MotionEvent.ACTION_UP:

            break;
        }

        return false;
    }

断点续传的关键在于发送RANGE值,以及建立随机访问文件对象,跳到文件末尾。 下面的代码是断线续传下载优酷的视频。测试方法:运行后,会循环输出当前下载的进度,在下载完之前强行停止(Eclipse上的红色Stop按钮),重新运行,可以看到续传效果。

package com.download;

import java.io.File;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;

public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        final String url = "http://f.youku.com/player/getFlvPath/sid/130586142897819_01/st/flv/fileid/03000202004DD41099B25204CFA0FDD01CCF75-CE87-989C-6950-ED1081070F59?K=b52bd8f2ccfb40d6161bacc0&hd=0";
        final String path = "c://1.flv";
        final long filesize;
        RemoteFile rf=getRemoteFile(url);
        long remoteSize=rf.size;
        final String realUrl=rf.realUrl;
        //得到重定向后的真实URL,经测试,优酷视频,如果不用真实URL下载,会报403错误
        System.out.println("真实URL"+realUrl);
        System.out.println("远程文件大小" + remoteSize);
        File f = new File(path);
        if (f.exists()) {
            filesize = f.length();
        } else {
            filesize = 0;
        }
        System.out.println("本地文件长度" + filesize);
        if (filesize < remoteSize) {
            new Thread() {
                public void run() {
                    try {
                        System.out.println("开始下载");
                        URL u = new URL(realUrl);
                        HttpURLConnection connection = (HttpURLConnection) u
                                .openConnection();
                        connection.setRequestProperty("RANGE", "bytes="+ filesize+"-");
                        //根据已下载的大小,设置下载的起点
                        connection.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)"); 
                        //设置UA
                        InputStream input = connection.getInputStream();
                        //读入InputStream
                        RandomAccessFile SavedFile = new RandomAccessFile(path, "rw");
                        //建立随机访问对象
                        SavedFile.seek(filesize);
                        //文件指针移动到文件末尾
                        byte[] b = new byte[1024];
                        //新建byte对象
                        int nRead;
                        long readed=filesize;
                        //readed用于存储已下载的字节数,所以初始值为文件大小
                        while ((nRead = input.read(b, 0, 1024)) > 0) {
                            //从InputStream循环读入byte对象,nRead为实际读入的byte对象长度
                            readed+=nRead;
                            //已下载长度计数
                            System.out.println(readed);
                            SavedFile.write(b, 0, nRead);
                            //把byte对象写入文件
                        }
                        connection.disconnect();
                        //断开连接
                        SavedFile.close();
                        //关闭文件
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }.start();
        }
    }

    public static RemoteFile getRemoteFile(String url) {
        long size = 0;
        String realUrl="";
        try {
            HttpURLConnection conn = (HttpURLConnection) (new URL(url))
                    .openConnection();
            size = conn.getContentLength();
            //远程文件体积
            realUrl=conn.getURL().toString();
            //真实URL
            conn.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
        RemoteFile rf=new RemoteFile(size,realUrl);
        //把取得的文件体积及真实URL返回
        return rf;
    }

}
class RemoteFile{
    //RemoteFile类,用于存储远程文件的大小及重定向后的地址
    long size;
    String realUrl;
    RemoteFile(long size,String realUrl){
        this.size=size;
        this.realUrl=realUrl;
    }
}

情况是这样的,我的Webview并不是写在XML文件中,而是在程序中动态生成,然后用addview方法加到UI中。加进去后一切都正常,但是当点到文件框时,却发现系统没有自动弹出输入法,也就无法输入任何文字。其实,这是因为刚加的view没有得到焦点,我们只需要在addview代码后面加一行,webview.requestFocus();即可解决问题

今天发现ListView如果显示的分割线,分割线粗会细不一致,两根粗线间隔着一根细线,网上搜索始终没有找到解决办法。我目前用隐藏分割线,然后在定义item视图的XML文件最后加了一个ImageView来解决,ImageView显示一张高为1像素的图片. 隐藏divider:

listview.setDivider(null);

添加ImageView:

<ImageView 
    android:layout_width="fill_parent" 
    android:layout_height="10dip" 
    android:src="@drawable/divider"
    />

刚开始学习Android开发的时候,因为学习GridView的第一个例子就是显示图片,所以误以为GridView是显示图片的控件。其实GridView是一个表格控件,功能类似HTML的table。下面用一个简单的例子来讲解GridView用法。 效果图: xml代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<GridView android:id="@+id/gv"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
/>
<Button android:id="@+id/b1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="两列"
/>
<Button android:id="@+id/b2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="四列"
/>
</LinearLayout>

一个GridView,两个按钮 在string.xml中定义一个数组,为GridView提供内容,也可以在JAVA程序中定义;

    <string-array name="date">
        <item>a</item>
        <item>b</item>
        <item>c</item>
        <item>d</item>
        <item>e</item>
        <item>f</item>
        <item>g</item>
        <item>h</item>
        <item>i</item>
    </string-array>

每一项的布局,在layout里新建一个xml文件,内容如下:

<?xml version="1.0" encoding="utf-8"?>
<TextView android:id="@+id/item"
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
</TextView>

这里可以定义显示的样式,如文字大小,颜色之类 最后是程序代码:

package com.pocketdigi.gridview;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.AdapterView.OnItemClickListener;

public class main extends Activity {
    /** Called when the activity is first created. */
    ArrayAdapter adapter;
    String[] s;
    GridView gv;
    Button b1,b2;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        gv=(GridView)findViewById(R.id.gv);
        b1=(Button)findViewById(R.id.b1);
        b2=(Button)findViewById(R.id.b2);
        s=getResources().getStringArray(R.array.date);
        adapter=new ArrayAdapter(this,R.layout.item,s);
        //显示文本适配器,如果需要显示其他如图片,可以自定义适配器
        gv.setAdapter(adapter);
        b1.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                gv.setNumColumns(2);
                //设置两列显示 
            }
            
        });
        b2.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                gv.setNumColumns(4);
                //设置四列显示
            }
            
        });
        gv.setOnItemClickListener(new OnItemClickListener(){

            @Override
            public void onItemClick(AdapterView arg0, View arg1, int arg2,
                    long arg3) {
                // TODO Auto-generated method stub
                setTitle("第"+String.valueOf(arg2)+"个:"+s[arg2]);
                //点击后在标题栏显示点击的项
            }
            
        });
        
    }
}

源代码打包下载: [download id=”15”]

系统为Mac OS 10.6.7,Eclipse为Galileo for php(自带PDT,不用装了,图个方便),在Windows下也是下的这个版本,解压完直接可以写JAVA,但今天在Mac OS下测试却不行.Eclipse可以正常运行,但是首选项里没有找到已经安装的JRE,自然也就无法编译运行.提示The specified JRE installation does not exist.我以为不能识别JRE,就装了个openJDK 1.7,结果在添加JRE的时候Eclipse却提示”Standard VM not Supported on MacOS”,无法添加JRE.经过大半天的搜索,总算在国外一论坛上找到解决方法:安装Eclipse Java Development Tools. 具体步骤:帮助-Install New Software-Work with选择 Galileo - http://download.eclipse.org/releases/galileo,根据Eclipse版本不同会有所区别.找到Eclipse Java Development Tools,应该在Programming Languages分类里,安装后重启Eclipse,再打开首选项,可以找到系统已经安装的JRE,问题解决.

2011.10.28注:如果需要控件停在动画后的位置,需要设置android:fillAfter属性为true,在set节点中。默认在动画结束后回到动画前位置。设置android:fillAfter后,我们看到了控件留在了动画后的位置,其实也只是看到在那个位置,真实位置还是在原来动画前那里,你会发现Button不能被点击,就是这个原因。所以我们可以在动画结束后,手动把控件移动到动画结束后的位置。这就需要根结点为AbsoluteLayout,因为LinearLayout不能通过x,y座标定位。具体方法:把布局换成AbsoluteLayout,使用Animation的setAnimationListener设置动画播放事件,在onAnimationEnd方法中,使用控件的setLayoutParams方法,设置动画后的位置。 5月15日注:overridePendingTransition只支持android 2.0以上版本 Android的动画效果分为两种,一种是tweened animation(补间动画),第二种是frame by frame animation。一般我们用的是第一种。补间动画又分为AlphaAnimation,透明度转换 RotateAnimation,旋转转换 ScaleAnimation,缩放转换 TranslateAnimation 位置转换(移动)。 动画效果在anim目录下的xml文件中定义,在程序中用AnimationUtils.loadAnimation(Context context,int ResourcesId)载入成Animation对象,在需要显示动画效果时,执行需要动画的View的startAnimation方法,传入Animation,即可。切换Activity也可以应用动画效果,在startActivity方法后,执行overridePendingTransition方法,两个参数分别是切换前的动画效果,切换后的动画效果,下面的例子中传入的是两个alpha动画,以实现切换Activity时淡出淡入,渐隐渐现效果。 下面贴出代码: 两个Activity的布局文件 main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/ll"
    android:background="@drawable/white"
    >
<TextView  android:id="@+id/tv"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="translate动画效果"
    />
<TextView  android:id="@+id/tv2"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="scale动画效果"
    />
<TextView  android:id="@+id/tv3"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="alpha动画效果"
    />
<TextView  android:id="@+id/tv4"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="rotate动画效果"
    />
<Button android:id="@+id/bt3"
     android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="动画演示"
/>
<Button android:id="@+id/bt"
     android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="切换"
/>
</LinearLayout>

activity2.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/ll2"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Activity2"
    />
<Button android:id="@+id/bt2"
     android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="返回main"
/>
</LinearLayout>

动画效果XML文件,全部存放在anim目录下: a1.xml 淡出效果

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha
    android:fromAlpha="1.0"
    android:toAlpha="0.0"
    android:duration="500"
    />
</set>
<!-- 
fromAlpha:开始时透明度
toAlpha:结束时透明度
duration:动画持续时间
 -->

a2.xml 淡入效果:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha
    android:fromAlpha="0.0"
    android:toAlpha="1.0"
    android:duration="500"
    />
</set>

rotate.xml 旋转效果:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromDegrees="300"
android:toDegrees="-360"
android:pivotX="10%"
android:pivotY="100%"
android:duration="10000" />
</set>
<!-- 
fromDegrees开始时的角度
toDegrees动画结束时角度
pivotX,pivotY不太清楚,看效果应该是定义旋转的圆心的
 -->

scale.xml 缩放效果:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
     <scale  
     android:interpolator= "@android:anim/decelerate_interpolator"    
     android:fromXScale="0.0"    
     android:toXScale="1.5"    
     android:fromYScale="0.0"    
     android:toYScale="1.5"    
     android:pivotX="50%"    
     android:pivotY="50%"    
     android:startOffset="0"    
     android:duration="10000"
     android:repeatCount="1"  
     android:repeatMode="reverse"
     /> 
</set>
<!-- 
interpolator指定动画插入器,常见的有加速减速插入器accelerate_decelerate_interpolator,加速插入器accelerate_interpolator,减速插入器decelerate_interpolator。
fromXScale,fromYScale,动画开始前X,Y的缩放,0.0为不显示,1.0为正常大小
toXScale,toYScale,动画最终缩放的倍数,1.0为正常大小,大于1.0放大
pivotX,pivotY动画起始位置,相对于屏幕的百分比,两个都为50%表示动画从屏幕中间开始
startOffset,动画多次执行的间隔时间,如果只执行一次,执行前会暂停这段时间,单位毫秒
duration,一次动画效果消耗的时间,单位毫秒,值越小动画速度越快
repeatCount,动画重复的计数,动画将会执行该值+1次
repeatMode,动画重复的模式,reverse为反向,当第偶次执行时,动画方向会相反。restart为重新执行,方向不变
 -->

translate.xml 移动效果:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="320"
android:toXDelta="0"
android:fromYDelta="480"
android:toYDelta="0"
android:duration="10000" />
</set>
<!-- 
fromXDelta,fromYDelta起始时X,Y座标,屏幕右下角的座标是X:320,Y:480
toXDelta,toYDelta动画结束时X,Y的座标
 -->

下面是程序代码,main.java:

package com.pocketdigi.animation;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextView;

public class main extends Activity {
    /** Called when the activity is first created. */
    TextView tv,tv2,tv3,tv4;
    Button bt3;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button bt=(Button)findViewById(R.id.bt);
        tv=(TextView)findViewById(R.id.tv);
        tv2=(TextView)findViewById(R.id.tv2);
        tv3=(TextView)findViewById(R.id.tv3);
        tv4=(TextView)findViewById(R.id.tv4);
        
        
        bt3=(Button)findViewById(R.id.bt3);
        bt.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent=new Intent(main.this,activity2.class);
                startActivity(intent);
                overridePendingTransition(R.anim.a2,R.anim.a1);
                //淡出淡入动画效果
            }
            
        });
        bt3.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Animation translate=AnimationUtils.loadAnimation(main.this, R.anim.translate);
                Animation scale=AnimationUtils.loadAnimation(main.this, R.anim.scale);
                Animation rotate=AnimationUtils.loadAnimation(main.this, R.anim.rotate);
                Animation alpha=AnimationUtils.loadAnimation(main.this, R.anim.a1);
                //载入XML文件成Animation对象
                tv.startAnimation(translate);
                tv2.startAnimation(scale);
                tv3.startAnimation(alpha);
                tv4.startAnimation(rotate);
                //应用动画
                
            }});
    }
}

activity2.java:

package com.pocketdigi.animation;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class activity2 extends Activity {
    Button bt2;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity2);
        bt2=(Button)findViewById(R.id.bt2);
        bt2.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent=new Intent(activity2.this,main.class);
                startActivity(intent);
                overridePendingTransition(R.anim.a2,R.anim.a1);
            }
            
        });
    }
}

注:动画切换Activity只有在新启动Activity才有效,如果Activity已经启动,并且intent加了FLAG_ACTIVITY_REORDER_TO_FRONT,这样不会新启动Activity,也就没有动画效果。 因为代码比较多,最后附上打包的源文件: [download id=”12”]

AlertDialog可以直接显示定义在XML中定义的数组,效果类似于在ListView上长按跳出的ContextMenu。 效果图: 实现代码:

package com.pocketdigi.alertdialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class main extends Activity {
    /** Called when the activity is first created. */
    Button b1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        b1=(Button)findViewById(R.id.b1);
        b1.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                AlertDialog.Builder ad=new AlertDialog.Builder(main.this);
                final String[] itemArray=getResources().getStringArray(R.array.item);
                //获取XML文件中数组
                ad.setTitle("标题");
                ad.setItems(R.array.item, new DialogInterface.OnClickListener() {
                    
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        Toast.makeText(main.this,itemArray[which],Toast.LENGTH_LONG).show();
                        //输出选中的项
                    }
                });
                ad.show();
            }});
        
        
    }
}

ProgressDialog,顾名思义,就是一个进度对话框,常用于显示载入进度、下载进度等。合理使用ProgressDialog能增加用户体验,让用户知道现在程序所处的状态。 下面是两种用法,第一种适合复杂环境,可以自定义风格,添加按钮等,而第二种只能简单的显示一个只有标题和信息的ProgressDialog

package com.pocketdigi.ProgressDialog;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Main extends Activity {
    /** Called when the activity is first created. */
    ProgressDialog pd1, pd2;
    Button b1, b2;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        b1 = (Button) findViewById(R.id.b1);
        b2 = (Button) findViewById(R.id.b2);
        b1.setOnClickListener(showPd1);
        b2.setOnClickListener(showPd2);
    }
    OnClickListener showPd1 = new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            pd1 = new ProgressDialog(Main.this);
            pd1.setTitle("PD1标题");
            pd1.setProgressStyle(ProgressDialog.STYLE_SPINNER);// 转圈风格
            // 进度条风格为ProgressDialog.STYLE_HORIZONTAL,使用setMax,setProgress,incrementProgressBy方法设置进度
            pd1.setMessage("PD1信息");
            pd1.setButton("关闭", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    pd1.dismiss();
                }
            
            });
            //可以使用setButton2,setButton3来添加更多按钮
            pd1.setCancelable(false);//不可被返回键取消对话框
            pd1.show();
        }

    };
    OnClickListener showPd2 = new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            pd2=ProgressDialog.show(Main.this,"PD2标题","PD2信息");
            //使用该方法将不能再设置按钮,适合显示简单的ProgressDialog
        }

    };

}

我们可以在程序中放入ttf字体文件,在程序中使用Typeface设置字体。 第一步,在assets目录下新建fonts目录,把ttf字体文件放到这。 第二步,程序中调用:

AssetManager mgr=getAssets();//得到AssetManager
Typeface tf=Typeface.createFromAsset(mgr, "fonts/ttf.ttf");//根据路径得到Typeface
tv.setTypeface(tf);//设置字体