高德地图黑屏问题
高德地图在Fragment中出现黑屏问题解决方案:
1、Fragment切换过程中不使用replace方法,使用show、hide方法即可解决每次切换地图后黑屏问题。但是首次切换至地图时依然会闪一下,请参照步骤2。
2、地图Fragment首次切换时出现闪屏,在Fragment所在Activity中添加getWindow().setFormat(PixelFormat.TRANSLUCENT)解决。
具体参照以下代码:
import android.content.Intent import android.graphics.Color import android.graphics.PixelFormat import android.os.Bundle import android.support.v4.app.FragmentTransaction import android.view.View import com.amap.api.location.AMapLocationClient import com.amap.api.location.AMapLocationClientOption import com.android.app.BaseActivity import com.android.ui.fragment.BoomFragment import com.android.ui.fragment.HomeFragment import com.android.ui.fragment.MapsFragment import com.jaeger.library.StatusBarUtil import com.sd2w.beilin.app.R import kotlinx.android.synthetic.main.activity_main.* /** * 应用启动引导页面 * * @author 祁连山 * @date 2017-09-18 * @version 1.0 */ class MainActivity : BaseActivity() { lateinit var locationClient: AMapLocationClient lateinit var clientOptions: AMapLocationClientOption var homeFragment = HomeFragment() var boomFragment = BoomFragment() var mapsFragment = MapsFragment() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) window.setFormat(PixelFormat.TRANSLUCENT) setContentView(R.layout.activity_main) StatusBarUtil.setColor(this, Color.parseColor("#318df0")) // 默认添加首页 getTransaction().add(R.id.id_main_holder, homeFragment).commit() id_main_group.check(R.id.id_radio_home) id_main_group.setOnCheckedChangeListener { group, checkedId -> when (checkedId) { R.id.id_radio_home -> { id_left_back.visibility = View.VISIBLE id_foo_text.text = "测试标题一" getTransaction().show(homeFragment).hide(boomFragment).hide(mapsFragment).commit() } R.id.id_radio_boom -> { id_left_back.visibility = View.INVISIBLE id_foo_text.text = "测试标题二" if (!boomFragment.isAdded) { getTransaction().add(R.id.id_main_holder, boomFragment).show(boomFragment).hide(homeFragment).hide(mapsFragment).commit() return@setOnCheckedChangeListener } getTransaction().hide(homeFragment).show(boomFragment).hide(mapsFragment).commit() } R.id.id_radio_map -> { id_left_back.visibility = View.VISIBLE id_foo_text.text = "测试标题三" if (!mapsFragment.isAdded) { getTransaction().add(R.id.id_main_holder, mapsFragment).show(mapsFragment).hide(homeFragment).hide(boomFragment).commit() return@setOnCheckedChangeListener } getTransaction().hide(homeFragment).hide(boomFragment).show(mapsFragment).commit() } } } id_right_img.setOnClickListener { var it = Intent(this, ProfileActivity::class.java) startActivity(it) } } fun getTransaction(): FragmentTransaction { return supportFragmentManager.beginTransaction() } }