Android常忘技能

一.设置字体

1.自定义字体:

在assets目录下新建fonts目录,把ttf字体文件放进去

AssetManager mgr=getAssets();//得到AssetManager
Typeface tf=Typeface.createFromAsset(mgr, "fonts/ttf.ttf");//根据路径得到Typeface
tv=findViewById(R.id.textview);tv.setTypeface(tf);//设置字体
2.使用RoBoto
android:fontFamily="sans-serif" // roboto regular 
android:fontFamily="sans-serif-light" // roboto light 
android:fontFamily="sans-serif-condensed" // roboto condensed  
android:fontFamily="sans-serif-thin" // roboto thin (android 4.2)  

//in combination with  
android:textStyle="normal|bold|italic"

可用的参数如下:
Regular
Italic
Bold
Bold-italic
Light
Light-italic
Thin
Thin-italic
Condensed regular
Condensed italic
Condensed bold
Condensed
bold-italic
3.设置粗体

xml中:

 android:textStyle=”bold”

中文需要代码动态设置

TextView tv = (TextView)findViewById(R.id.TextView01);   
//中文仿“粗体”--使用TextPaint的仿“粗体”设置setFakeBoldText为true。
 tv.getPaint().setFakeBoldText(true);

二.列表排序

通过Collections.sort(List list, Comparator c);来进行对对象的简单排序

  /**
    * 大于0表示前一个数据比后一个数据大 (从小到大)
    * 0表示相等
    * 小于0表示前一个数据小于后一个数据(从大到校)
    */
   Comparator comparator = new Comparator() {
                    @Override
                    public int compare(Bean o1, Bean o2) {
                        int i = 0;
                        long result = o1.getTime() - o2.getTime();
                        if (result != 0) {
                            i = result > 0 ? -1 : 1;
                        }
                        return i;
                    }
                };
   Collections.sort(list, comparator);

不判断等于0而直接使用正则表达式返回1或者-1会在某些机上导致crash,具体跟jdk版本有关系,忘记crash叫什么名字了

若要使用Comparable来进行排序,则让Bean implements Comparable后重写compareTo(Bean anOther Bean)就行。

倒序

Collections.reverse(list);

你可能感兴趣的:(Android常忘技能)