Henji项目
1 关于Mapbox的token
1.1 勾选权限
token新建时,必须要勾选Download:Read,引导说明里有提到。否则不能下载引用包。
1.2 string固定命名
<string name="mapbox_access_token">YOUR_MAPBOX_ACCESS_TOKEN</string>
上述定义,name必须是mapbox_access_token,否则报错。
2 页面优化
2.1 标题栏清除
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// 去除导航栏
val actionBar = supportActionBar
actionBar?.hide()
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
在setContentView之前,以上两句即可。
2.2 状态栏透明
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
// 在此使用
setHalfTransparent();
setFitSystemWindow(false);
函数如下,引用自:
https://www.jianshu.com/p/e89ee0a77bb5。
/**
* 全透状态栏
*/
protected fun setStatusBarFullTransparent() {
if (Build.VERSION.SDK_INT >= 21) { //21表示5.0
val window = window
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
or View.SYSTEM_UI_FLAG_LAYOUT_STABLE)
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
window.statusBarColor = Color.TRANSPARENT
} else if (Build.VERSION.SDK_INT >= 19) { //19表示4.4
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
//虚拟键盘也透明
//getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
}
/**
* 半透明状态栏
*/
protected fun setHalfTransparent() {
if (Build.VERSION.SDK_INT >= 21) { //21表示5.0
val decorView = window.decorView
val option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
decorView.systemUiVisibility = option
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
} else if (Build.VERSION.SDK_INT >= 19) { //19表示4.4
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
//虚拟键盘也透明
// getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
}
/**
* 如果需要内容紧贴着StatusBar
* 应该在对应的xml布局文件中,设置根布局fitsSystemWindows=true。
*/
private var contentViewGroup: View? = null
protected fun setFitSystemWindow(fitSystemWindow: Boolean) {
if (contentViewGroup == null) {
contentViewGroup = (findViewById<View>(android.R.id.content) as ViewGroup).getChildAt(0)
}
contentViewGroup!!.fitsSystemWindows = fitSystemWindow
}
2.3 状态栏清除和恢复
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Configuration mConfiguration = this.getResources().getConfiguration(); //获取设置的配置信息
Toast.makeText(getApplicationContext(), "旋转成功。", Toast.LENGTH_SHORT).show(); // 在这里添加屏幕切换后的操作
int ori = mConfiguration.orientation; //获取屏幕方向
//隐藏状态栏
if(ori == mConfiguration.ORIENTATION_LANDSCAPE) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
navView.setVisibility(View.GONE);
}else {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); //显示状态栏
navView.setVisibility(View.VISIBLE);
}
}
3 Java调用Kotlin方法
3.1 出现“找不到符号”的问题
原因:
在Studio中没做kotlin的配置
解决方法:
- 在app下的build.gradle中的plugins属性中添加以下两行
plugins {
id 'kotlin-android'
id 'kotlin-android-extensions'
}
- 在project下的build.gradle中加入
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.0"
}
4 Kotlin与Java的一些不同
4.1 类和函数的定义
class BackAbleActivity : FragmentActivity(), OnAddressPickedListener {
fun goBack(view: View?) {
finish()
}
}
public class BackAbleActivity extends FragmentActivity implements OnAddressPickedListener {
public void goBack(View view) {
finish();
}
}
5 获取Activity
((MainActivity) activity). //Java 调用activity方法
(activity as MainActivity?)!! // Kotlin 调用activity方法
this.context
this
this.activity()