Android 适配刘海屏,挖孔屏,珍珠屏,水滴屏到此为止!

3,783 阅读2分钟

今天都2020年5月16号了,还在为适配Android各种异形屏发愁吗?最近写了一个开源库得到身边朋友的一致认可,今天就在这里献丑了,有问题欢迎在git地址上提issue,我会及时修复。

需求

假如有一个控件CommonTitleView。一行代码就可以解决各种屏幕适配就完美了。

解决思路

首先将activity设置为沉浸式布局,用一个控件的上半部分来适配异形屏,下半部分来设置title的内容

使用

开源代码量很少,有代码洁癖的可以去git地址拷贝源码,到你自己项目中使用。

简单用法:

引入依赖
implementation 'com.github.AndyFrist:DemoList:1.0.2'
设置主题
 <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
布局文件activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">

    <com.andyfrist.fitstatusbarlibrary.CommonTitleView
        android:id="@+id/commonTitleView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:back_Text="取消"
        app:back_img="@drawable/ic_back"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:right_Text="分享"
        app:right_color="@color/colorPrimaryDark"
        app:right_img="@mipmap/ic_category_0"
        app:status_color="@color/colorPrimaryDark"
        app:title_content_Text="这是中间文字" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="
        左一:图片按钮,设置back_img属性即显示,否则Gone\n\n
        左二:文字按钮,设置back_Text属性即显示,否则Gone,支持设置文字颜色和大小\n\n
        右一:图片按钮,设置right_img属性即显示,否则Gone\n\n
        右二:文字按钮,设置right_Text属性即显示,否则Gone,支持设置文字颜色和大小\n\n"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java 中使用
class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        StatusBarUtil.translucentStatusBar(this)
        setContentView(R.layout.activity_main)
//返回按钮有默认finish功能,当然也可以复写点击事件
//        commonTitleView.setTitleBackIvClickListener { finish() }
        commonTitleView.setTitleBackTvClickListener { Toast.makeText(this,"取消",Toast.LENGTH_LONG).show()  }
        commonTitleView.setTitleRightIvClickListener { startActivity(Intent(this,TitleActivity::class.java)) }
        commonTitleView.setTitleRightTvClickListener { Toast.makeText(this,"分享",Toast.LENGTH_LONG).show() }
    }
}
小米8截图效果:

这就完事了哦,核心代码就一句:

//设置activity沉浸布局
 StatusBarUtil.translucentStatusBar(this)

控件也就一个CommonTitleView,有些同学可能会说,我们的产品经理的需求的title比这个变态多了好嘛,可能会是各种花里花哨的控件和一些未知的骚操作。怎么破~~~好的,这里也有满足动态设置的方法

稍高级用法:

引入依赖和设置主题同上面一样,只不过用了两个可变参数的方法,可以左右都动态设置控件。方法名字如下:

titleMain.addViewAttitleLeftLL(imageView, textView, checkBox)
titleMain.addViewAttitleRightLL(imageView, textView, checkBox)
布局文件activity_title.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".TitleActivity">

    <com.andyfrist.fitstatusbarlibrary.CommonTitleView
        android:id="@+id/titleMain"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:status_color="@android:color/holo_red_dark"
        app:title_color="@color/colorPrimary"
        app:title_content_Text="中间文案" />

</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java 中使用

class TitleActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        StatusBarUtil.translucentStatusBar(this)
        setContentView(R.layout.activity_title)

        val imageView = ImageView(this)
        val imageViewparams: LinearLayout.LayoutParams = LinearLayout.LayoutParams(
            StatusBarUtil.dp2px(this, 30f),
            StatusBarUtil.dp2px(this, 30f)
        )
        imageViewparams.gravity = Gravity.CENTER_VERTICAL
        imageViewparams.marginStart = StatusBarUtil.dp2px(this, 15f)
        imageView.layoutParams = imageViewparams
        imageView.setImageResource(R.mipmap.ic_category_0)

        val textView = TextView(this)
        val textparams: LinearLayout.LayoutParams = LinearLayout.LayoutParams(
            StatusBarUtil.dp2px(this, 30f),
            StatusBarUtil.dp2px(this, 30f)
        )
        textparams.gravity = Gravity.CENTER_VERTICAL
        textparams.marginStart = StatusBarUtil.dp2px(this, 15f)
        textView.layoutParams = textparams
        textView.gravity = Gravity.CENTER_VERTICAL
        textView.setText("TextView")

        titleMain.addViewAttitleRightLL(textView,imageView)


        val checkBox = CheckBox(this)
        val checkBoxparams: LinearLayout.LayoutParams = LinearLayout.LayoutParams(
            StatusBarUtil.dp2px(this, 130f),
            StatusBarUtil.dp2px(this, 30f)
        )
        checkBoxparams.gravity = Gravity.CENTER_VERTICAL
        checkBoxparams.marginStart = StatusBarUtil.dp2px(this, 1f)
        checkBox.setText("同意")
        checkBox.layoutParams = checkBoxparams

        checkBox.setOnCheckedChangeListener { compoundButton, b ->
            if (b){
                Toast.makeText(this,"同意",Toast.LENGTH_LONG).show()
            }else{
                Toast.makeText(this,"同意",Toast.LENGTH_LONG).show()
            }
        }

        val button = Button(this)
        button.text = "按钮"
        titleMain.addViewAttitleLeftLL( button, checkBox)
        button.setOnClickListener { finish() }

    }
}

小米8截屏效果图

好了。所有内容结束了把sample地址给上