Tips of Android(不定时更新中)

来源:互联网 发布:金庸群侠传x 原版数据 编辑:程序博客网 时间:2024/04/28 03:09

一、向android工程中引入特殊字体

引入字体(ttf,otf均可):
在android工程的src文件夹下面建立assets文件夹。如图:
这里写图片描述
建立好之后在app的gradle里面声明assets文件夹的所在目录
这里写图片描述
使用方式:

        val typeFace:Typeface = Typeface.createFromAsset(assets, "fofbb_ital.otf")        appName.typeface = typeFace

参考:
1、https://stackoverflow.com/questions/37301876/using-otf-type-fonts-in-android-studio
2、http://blog.csdn.net/wei_chong_chong/article/details/52250030

二、Android应用启动白色背景

不知道大家有没有留心,手机上的很多应用刚刚启动的时候是白色背景,之后才会跳转到splashActivity,变成我们希望的过渡样式。这样的感觉总归不够好。

方法:
定义SplashTheme来设置windowBackground

    <style name="SplashTheme" parent="Theme.AppCompat.Light.NoActionBar">        <item name="android:windowTranslucentStatus">false</item>        <item name="android:windowTranslucentNavigation">true</item>        <item name="android:statusBarColor">@android:color/transparent</item>        <item name="windowActionBar">false</item>        <item name="windowNoTitle">true</item>        <item  name="android:windowBackground">@color/colorPrimary</item>    </style>

定义完成之后,对于启动activity应用这个Theme
参考:http://blog.csdn.net/anonymous_me/article/details/52748660

三、向AndroidStudio里面引入library流程

http://blog.csdn.net/u014772414/article/details/51194952

四、沉浸式体验的实现(不要叫做沉浸式状态栏!!!)

沉浸式体验目前大致分为两种:

1:完全沉浸式,完全沉浸式是隐藏了状态栏,从而让用户更好的沉浸到应用里面。
这类应用一般是游戏应用和视频应用。

效果如图:
这里写图片描述
这种情况下时重写activity的onWindowFocusChanged函数即可。

    //全屏播放    override fun onWindowFocusChanged(hasFocus: Boolean) {        super.onWindowFocusChanged(hasFocus)        if (hasFocus && Build.VERSION.SDK_INT >= 19) {            val decorView = window.decorView            decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or                    View.SYSTEM_UI_FLAG_FULLSCREEN or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY        }    }

详细请参考郭霖大神的博客:http://blog.csdn.net/guolin_blog/article/details/51763825

2、第二种沉浸式,保留状态栏,但是状态栏的背景设置为透明。
沉浸式是在android4.4(api19)之后才有的。我也是个android初学者,在写android的时候并没有去照顾android的多版本,所以在这里我只给出api21之后的实现方法。

先上图,实现效果如下:
这里写图片描述

为了实现这个效果我查看了很多博客,可能是看博客的时候不够仔细,所以实现这个效果的过程并不简单,我这么说不是因为这个效果难实现,实现这个只需要两个步骤就行了。

1:在activity的xml布局时在根布局中设置对应的android:fitsSystemWindows属性。

fitSystemWindows属性的含义:

官方描述:

Boolean internal attribute to adjust view layout based on system windows such as the status bar. If true, adjusts the padding of this view to leave space for the system windows. Will only take effect if this view is in a non-embedded activity.

简单描述:

这个一个boolean值的内部属性,让view可以根据系统窗口(如status bar)来调整自己的布局,如果值为true,就会调整view的paingding属性来给system windows留出空间….

实际效果:

当status bar为透明或半透明时(4.4以上),系统会设置view的paddingTop值为一个适合的值(status bar的高度)让view的内容不被上拉到状态栏,当在不占据status bar的情况下(4.4以下)会设置paddingTop值为0(因为没有占据status bar所以不用留出空间)。
一般我们想实现沉浸式是想把状态栏的背景改成我们想显示控件的背景,所以我们还需要在我们想要显示的控件的布局以及控件的父布局里面也要加入: android:fitsSystemWindows=”true”

“Talk is Cheap,show me the code”

<?xml version="1.0" encoding="utf-8"?><android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:fitsSystemWindows="true"    android:id="@+id/songSheetActivity"    tools:context="com.example.xiaojun.kotlin_try.ui.activity.music.SongSheetActivity">    <!--//-->    <android.support.design.widget.AppBarLayout        android:id="@+id/songSheetAppBar"        android:fitsSystemWindows="true"        android:layout_width="match_parent"        android:layout_height="270dp"        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">        <!--//-->        <android.support.design.widget.CollapsingToolbarLayout            android:id="@+id/songSheetCollTool"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:fitsSystemWindows="true"            app:expandedTitleMarginEnd="64dp"            app:expandedTitleMarginStart="48dp"            app:contentScrim="@color/colorPrimary"            app:statusBarScrim="@android:color/transparent"            app:layout_scrollFlags="scroll|exitUntilCollapsed">           <include layout="@layout/song_list_operation"/>            <android.support.v7.widget.Toolbar                android:id="@+id/songSheetToolbar"                android:layout_width="match_parent"                android:layout_height="?attr/actionBarSize"                app:navigationIcon="@drawable/ic_action_back"                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"                app:layout_collapseMode="pin">            </android.support.v7.widget.Toolbar>        </android.support.design.widget.CollapsingToolbarLayout>    </android.support.design.widget.AppBarLayout>    <android.support.v4.widget.NestedScrollView        android:id="@+id/musicSheetNest"        android:layout_width="match_parent"        android:layout_height="match_parent"        app:layout_behavior="@string/appbar_scrolling_view_behavior">        <LinearLayout            android:orientation="vertical"            android:layout_width="match_parent"            android:layout_height="match_parent">            <include layout="@layout/song_list_play"/>            <android.support.v7.widget.RecyclerView                android:id="@+id/songListRecyclerView"                android:layout_width="match_parent"                android:layout_height="wrap_content"                app:layout_behavior="@string/appbar_scrolling_view_behavior">            </android.support.v7.widget.RecyclerView>        </LinearLayout>    </android.support.v4.widget.NestedScrollView></android.support.design.widget.CoordinatorLayout>

这个布局里面CollapsingToolbarLayout 内部include的内容是我的歌单背景,也就是第二个效果图。include的布局如下:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:background="@android:color/transparent"    android:fitsSystemWindows="true"    android:layout_width="match_parent"    android:layout_height="match_parent">    <ImageView        android:id="@+id/songListBg"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:scaleType="centerCrop"        android:background="@drawable/temp"/>    <ImageView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:scaleType="centerCrop"        android:src="@drawable/abc_list_selector_disabled_holo_dark"/> </RelativeLayout>

2、为activity新建theme文件,并在manifests里面设置,这个的目的是把状态栏的背景设置为透明:

<style name="TranslucentTheme" parent="Theme.AppCompat.Light.NoActionBar">        <item name="android:windowTranslucentStatus">false</item>        <item name="android:windowTranslucentNavigation">true</item>        <!--Android 5.x开始需要把颜色设置透明,否则导航栏会呈现系统默认的浅灰色-->        <item name="android:statusBarColor">@android:color/transparent</item></style>

沉浸式实现成功
参考:
1、http://blog.csdn.net/fan7983377/article/details/51604657
2、http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/1122/3712.html
3、http://www.jianshu.com/p/2db7ecc50495

五、单例模式写法

这是直接从这个博客里面直接引用过来的
http://cantellow.iteye.com/blog/838473

public class Singleton {      private volatile static Singleton singleton;      private Singleton (){}      public static Singleton getSingleton() {      if (singleton == null) {          synchronized (Singleton.class) {          if (singleton == null) {              singleton = new Singleton();          }          }      }      return singleton;      }  }  

Parcelable和Serializable

1、区别
Parcelable:
Parcelable是android特有的序列化功能,可用于intent传递,也可以用于进程之间的通信(IPC),Parcelable接口实现相对于Serializable要复杂一些,但是使用内存的时候Parcelable的效率更高,所以推荐使用Parcelable。但是如果要将数据储存在磁盘上,或者序列化后用于网络传输Parcelable实现比较复杂,而且Parcelable在外界有变化的情况下,不能很好的保持数据的持续性,所以这个时候更建议使用Serializable。

Serializable:javaSE提供的序列化方式。Serializable在序列化时会产生大量的临时变量,引起频繁的GC,效率偏低。但是实现简单,只需要实现Serializable接口就行了。

2、使用方法
在这里不介绍Serializable的实现方法,只介绍Parcelable的使用方法

1、实现步骤
(1)、重写writeToParcel方法,将你的对象序列化为一个Parcel对象,即:将类的数据写入外部提供的Parcel中,打包需要传递的数据到Parcel容器保存,以便从Parcel容器获取数据。
(2)、重写describeContents方法,内容接口描述,基本不用管,默认返回0就可以。
(3)、实例化静态内部对象CREATOR实现接口Parcelable.Creator

2、实例

public class User implements Parcelable {    private String userName;    private String userPass;    private int userId;//此方法返回几乎都默认返回0,除了当前对象存在文件描述符的时候返回1    @Override    public int describeContents() {        return 0;    }    @Override    public void writeToParcel(Parcel parcel, int i) {        parcel.writeString(userName);        parcel.writeString(userPass);        parcel.writeInt(userId);    }    protected User(Parcel in) {        userId = in.readInt();        userName = in.readString();        userPass = in.readString();    }    public static final Creator<User> CREATOR = new Creator<User>() {        @Override        public User createFromParcel(Parcel in) {            return new User(in);        }        @Override        public User[] newArray(int size) {            return new User[size];        }    };}

备注:

在此为大家宣传一下我的新开源项目Kotlin_Try。我承认这个应用名字非常土鳖,不过里面的内容还是可以的。上面的截图以及代码均是来自Kotlin_try。如果大家感兴趣的话欢迎fork和star

https://github.com/KingLanding94/Kotlin_Try/blob/master/README.md

原创粉丝点击