博采众长,精于一技。Live for love, work for dream.

[Android开发]修复“Debug certificate expired on” 错误信息

今天打开ADT,所有的项目都显示错误。
出错提示:
“Error generating final archive: Debug certificate expired on …”
搜索了一番,找到了解决办法。

找到这个该死的文件:“debug.keystore”
一般躲在:
“~/.android” (OSX, Linux).
“C:\Users\

删掉"debug.keystore",然后Clean一下项目就OK了。
不过还遗留一个问题。365天之后,这个错误又会重新出现。
治根的办法,自己手动生成一个,过期期限为1000天(生成改文件时请先删除"debug.keystore"):
keytool -genkey -keypass android -keystore debug.keystore -alias androiddebugkey -storepass android -validity 1000 -dname “CN=Android Debug,O=Android,C=US”

参考文章:http://www.androidian.de/?p=940

[Android开发]自定义CheckBox

在Android开发中,系统自带的默认CheckBox由于比较简陋,可能难以满足部分人的审美需求,不过,Android具有很强的扩展性,自定义CheckBox其实也很简单。
1. Layout中定义CheckBox
<CheckBox android:textSize="14.0sp" android:id="@id/my_checkbox" android:background="@null"  android:layout_width="wrap_content" android:layout_height="wrap_content" android:button="@drawable/cb_selector" android:text="自定义CheckBox" />

2. 然后定义cb_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector
  xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="false" android:drawable="@drawable/cb_unchecked" />
    <item android:state_checked="true" android:drawable="@drawable/cb_checked" />
</selector>

这里需要自定义两张图片,cb_unchecked.png 和 cb_checked.png分别代表未选中和选中状态。

[Android开发]进度条对话框

在Android开发中,要做一些Loading的提示,这就需要用到进度条对话框(ProgressDialog),Android ProgressDialog继承于AlertDialog,实现方式有两种。先上图,有图有真相。
效果一:
Android ProgressDialog
效果二:
Android ProgressDialog

效果一实现代码:
ProgressDialog MyDialog = ProgressDialog.show( MyActivity.this, " " , " Loading. Please wait ... ", true);

效果二实现代码:
ProgressDialog pbarDialog;
pbarDialog = new ProgressDialog( mContext );
pbarDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pbarDialog.setMessage("Loading...");
pbarDialog.setCancelable(false);

[Android开发]ScrollView嵌套GridView的解决办法

前些日子在开发中用到了需要ScrollView嵌套GridView的情况,由于这两款控件都自带滚动条,当他们碰到一起的时候便会出问题,即GridView会显示不全。

解决办法,自定义一个GridView控件

public class MyGridView extends GridView {
    public MyGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyGridView(Context context) {
        super(context);
    }

    public MyGridView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int expandSpec = MeasureSpec.makeMeasureSpec(
                Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }
}

该自定义控件只是重写了GridView的onMeasure方法,使其不会出现滚动条,ScrollView嵌套ListView也是同样的道理,不再赘述。

XML布局代码
<ScrollView android:layout_height="wrap_content"
        android:layout_width="fill_parent" android:id="@+id/scroll_content">
        <com.yourclass.MyGridView xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/grid_view" android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:numColumns="auto_fit"
            android:horizontalSpacing="1dip" android:verticalSpacing="1dip"
            android:columnWidth="150dip" android:stretchMode="columnWidth"
            android:gravity="center">
            
        </com.yourclass.MyGridView>
    </ScrollView>

Java调用代码
MyGridView gridview = (MyGridView) findViewById(R.id.grid_view);
gridview.setAdapter(new ImageAdapter(this));

[Android]在OnCreate()中播放Animation动画

默认情况下,不能在OnCreate()中执行animation.start();是无效的,因为在OnCreate()中AnimationDrawable还没有完全的与ImageView绑定,在OnCreate()中启动动画,就只能看到第一张图片。
要实现OnCreate()加载完立即播放动画怎么办呢?解决办法是,用Handler。
package info.rebill.animation;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ImageView;

public class Main extends Activity {
    private ImageView btnPlay;
    private AnimationDrawable playAnim;
    private Handler handler = new Handler();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        btnPlay = (ImageView) findViewById(R.id.btn_play);
        handler.postDelayed(new Runnable() {
            public void run() {
                playAnim = (AnimationDrawable) btnPlay.getBackground();
                playAnim.start();
            }
        }, 50);
    }
}

返回顶部