近来颇有空闲,于是建了一个项目,写一些小demo练练手。目前(2017年7月20日22:59:50)已经完成了3个,Github地址点这里。
首先是项目结构,并没有任何mvpmvcmvvm,就随便分了下包

看名字就知道干什么的,就不赘述了。
首先是首页的设计。俗话说人活一张脸树活一张皮,好看养活APP,虽然做不出什么鬼斧神工精妙绝伦刀光剑影行侠仗义太精美的界面,但是太丑的话是断然不能忍受的。 于是厚着脸皮找公司的UI射鸡湿要图,结果换来一句很鄙视的“你照着qq做不就行了”,忍气吞声的我竟觉得这主意还不错。

那我要实现的效果是这样的:

但实际初始化界面是这样的:
(没图)
首先新建一个HomeActivity,建立布局home_activity.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical">
<LinearLayout
android:id="@+id/toolbar"
style="@style/BaseToolbarStyle">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical|left"
android:gravity="center"
android:text="@string/home_page"
android:textColor="#fff"
android:textSize="36px" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
顶上是一个假的Toolbar,下面是一个RecyclerView,以后的Demo就添加到这个RecyclerView里面了。接下来首先要做的当然是写一个BaseActivity,做一些重复的初始化操作,让以后所有的Activity继承自他。其实并没有什么骚操作
public abstract class BaseActivity extends AutoLayoutActivity {
private static final String TAG = "BaseActivity";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(getContentView());
ButterKnife.bind(this);
initView();
}
/**
* 初始化界面
*/
protected abstract void initView();
/**
* @return Activity的布局文件id
*/
protected abstract int getContentView();
}
这个AutoLayoutActivity是什么呢?这是CSDN鸿洋大佬做的一个开源项目,直接在布局里写固定的像素,他会自动帮你适配屏幕,可以说是非常diao了。
BaseActivity里面做了两件事,第一是setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
固定竖屏,第二是ButterKnife.bind(this);
初始化ButterKnife。
另外把onCreate拆成了initView和getContentView两个abstract方法,继承自BaseActivity的Activity需要实现他们多此一举
接下来就开始写首页了。
这是Android Studio自动新建的布局文件:

要实现上面需要的样子,至少有两点要实现的:
- 头部的ActionBar渐变效果
- 状态栏透明
观察QQ的头部,可以看到是深蓝-浅蓝-深蓝的渐变色,浅蓝的部位大概在70%,那么新建一个drawable文件,实现该渐变:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:centerColor="@color/blueGradientCenter"
android:centerX="0.7"
android:endColor="@color/blueGradientEnd"
android:startColor="@color/blueGradientStart" />
</shape>
这三个blue是我找UI射鸡湿帮我取的
效果图:

嗯,第一项完成。
状态栏透明,因为以前做过这个,以为驾轻就熟,于是坑来了。
首先在AppTheme里面加入
<item name="android:fitsSystemWindows">false</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
效果图如下:

看起来一切正常,真机测试的时候大部分机型都正常,但是在Android 7.0的华为荣耀8上面的显示却是这样的:

并没有完全透明!于是在网上寻找解决方案。
Android 系统状态栏沉浸式/透明化完整解决方案
Android透明状态栏与沉浸模式全解
这类型的文章太多了,随便一搜就是一大波,解决方案是找到了,在onCreate中加入:
Window window = getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
| WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
window.setStatusBarColor(Color.TRANSPARENT);
window.setNavigationBarColor(Color.TRANSPARENT);
这样的话,荣耀8倒是显示正常了,但是另一台华为畅想5s却变成了这样:

状态栏是透明了,可惜一下子透到了桌面上(上面的屎红色是桌面颜色),华为你这不是坑爹吗?无奈,不明原因,猜测可能是Android 7.0对状态栏的android:windowTranslucentStatus属性做了改变,于是把上面的代码改成了
if (Build.VERSION.SDK_INT >= 24) {
Window window = getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
| WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
window.setStatusBarColor(Color.TRANSPARENT);
window.setNavigationBarColor(Color.TRANSPARENT);
}
由于没有更多的手机了,至少在目前手上的手机上面都显示正常了,先凑合用着吧!
直到后来我发现了这是华为系统的问题:沉浸式状态栏实现及遇到的坑假装没看见
好了,上面这段代码加入到BaseActivity中,就大功告成了
下面的RecyclerView就不赘述了,每个Item对应一个功能,最终的首页界面是这样的:

虽然还是总感觉有些奇怪但是总归是凑合能看吧。
网友评论