Android开发百科全书②

来源:互联网 发布:华为软件视频会议 编辑:程序博客网 时间:2024/05/24 04:25

Android应用打破65K方法数限制

1.修改Gradle配置文件,启用MultiDex并包含MultiDex支持:

android { compileSdkVersion 21 buildToolsVersion "21.1.0"defaultConfig {    ...    minSdkVersion 14    targetSdkVersion 21    ...    // Enabling multidex support.    multiDexEnabled true}...}dependencies { compile 'com.android.support:multidex:1.0.0' } 

2.让应用支持多DEX文件。在MultiDexApplication JavaDoc中描述了三种可选方法:

在AndroidManifest.xml的application中声明android.support.multidex.MultiDexApplication;
如果你已经有自己的Application类,让其继承MultiDexApplication;
如果你的Application类已经继承自其它类,你不想/能修改它,那么可以重写attachBaseContext()方法:

@Override protected void attachBaseContext(Context base) {    super.attachBaseContext(base); MultiDex.install(this);}

经过以上步骤,你的应用已经可以实现多个DEX文件了。当应用构建时,构建工具会分析哪些类必须放在第一个DEX文件,哪些类可以放在附加的DEX文件中。当它创建了第一个DEX文件(classes.dex)后,如果有必要会继续创建附加的DEX文件,如classes2.dex, classes3.dex。Multidex的支持类库将被包含在应用的第一个DEX文件中,帮助实现对其它DEX文件的访问。

把String字符串转化为drawable设置成TextView的drawableRight

设置TextView的drawable:

Drawable drawableleft = getResources().getDrawable(          R.drawable.icon_qq_gray);  drawableleft.setBounds(0, 0, drawableleft.getMinimumWidth(),          drawableleft.getMinimumHeight());  txt_bind_qq.setCompoundDrawables(drawableleft, null,          TextToDrawable(R.string.bound), null);  把字符串转化为drawable:[java] view plain copypublic Drawable TextToDrawable(int id) {      Bitmap bitmap = Bitmap.createBitmap(200, 250, Config.ARGB_8888);      Canvas canvas = new Canvas(bitmap);      Paint paint = new Paint();      paint.setTextSize(65);      paint.setTextAlign(Align.LEFT);      paint.setColor(Color.GRAY);      String s = getResources().getString(id);      FontMetrics fm = paint.getFontMetrics();      canvas.drawText(s, 0, 145 + fm.top - fm.ascent, paint);      canvas.save();      Drawable drawableright = new BitmapDrawable(bitmap);      drawableright.setBounds(0, 0, drawableright.getMinimumWidth(),              drawableright.getMinimumHeight());      return drawableright;  }  

让listview在scrollview中自由滑动

http://blog.csdn.net/wanghao200906/article/details/51084975

mipmap和drawable文件夹的区别

定位不同
mipmap文件夹下的图标会通过Mipmap纹理技术进行优化。关于Mipmap纹理技术的介绍,请参考:Mipmap纹理技术简介

经过查询官方和第三方资料,得出结论:

mipmap文件夹下,仅仅建议放启动图标/app launcher icons,也就是应用安装后,会显示在桌面的那个图标。而其他的图片资源等,还是按照以前方式,放在drawable文件夹下。
下面再详细阐述下,得出以上结论的依据:
1.google官方关于mipmap和drawable的定位

drawable/
For bitmap files (PNG, JPEG, or GIF), 9-Patch image files, and XML files that describe Drawable shapes or Drawable objects that contain multiple states (normal, pressed, or focused). See the Drawable resource type.

mipmap/
For app launcher icons. The Android system retains the resources in this folder (and density-specific folders such as mipmap-xxxhdpi) regardless of the screen resolution of the device where your app is installed. This behavior allows launcher apps to pick the best resolution icon for your app to display on the home screen. For more information about using the mipmap folders, see Managing Launcher Icons as mipmap Resources.
2.stackoverflow上关于mipmap和drawable的区别

The mipmap folders are for placing your app icons in only. Any other drawable assets you use should be placed in the relevant drawable folders as before.
用法不同
以ic_launcher为例。
1.放在mipmap文件夹下时,引用方式如下:

android:icon=”@mipmap/ic_launcher”

R.mipmap.ic_launcher
2.放在drawable文件夹下时,引用方式如下:

android:icon=”@drawable/ic_launcher”

R.drawable.ic_launcher
参考
Mipmap纹理技术简介
mipmap vs drawable folders
google官方关于mipmap和drawable的定位

关于Android app首次安装完成后在安装界面直接“打开”应用再按home键返回桌面,重新进入app重复实例化launcher activity的问题的解决

http://blog.csdn.net/love100628/article/details/43238135

动化快速实现Parcelable接口序列化

http://blog.csdn.net/kroclin/article/details/40902721
二、效果展示

首先建好一个测试用的person实体类:
在线安装
然后打开File -> Settings -> Pugins -> Browse Repositories 如下,输入android parcelable code generator:
然后就可以安装了,安装好了之后重启下就可以用啦~~
使用方法:
新建好一个实体类后写好属性:
按下Alt+Insert,选择Palcelable,选择需要的属性,按下OK,搞定.

Android studio - SVN 使用教程

https://www.zhihu.com/question/32298079
http://ask.android-studio.org/?/article/97
http://ask.android-studio.org/?/question/10
https://my.oschina.net/fyyy/blog/519353

单例模式的一种写法

public class SingletonTest {    private static final Singleton<SingletonTest> gDefault = new Singleton<SingletonTest>(){        @Override        protected SingletonTest create() {            return new SingletonTest();        }    };    private SingletonTest(){}    public static SingletonTest getInstance(){        return gDefault.get();    }}