It’s May 16, 2020, and you’re still having trouble adapting to Android’s various alien screens? Recently, I wrote an open source library which has been unanimously recognized by my friends. Today, I would like to make a fool of myself here. If there is any problem, please make an issue on git address and I will fix it in time.

demand

Suppose you have a control called CommonTitleView. One line of code can solve all kinds of screen adaptation is perfect.

solution

First, set the activity to an immersive layout, using the top half of a control to fit the profiled screen, and the bottom half to set the title content

use

The amount of open source code is very small, code cleanliness can go to the Git address copy source code, to use your own projects.

Simple usage:

Introduction of depend on
Implementation 'com. Making. AndyFrist: DemoList: 1.0.2'Copy the code
Set the theme
 <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
Copy the code
The layout file activity_main.xml
<? The 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=" cancel "app:back_img="@drawable/ic_back" App :layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:right_Text=" share" app:right_color="@color/colorPrimaryDark" app:right_img="@mipmap/ic_category_0" App :status_color="@color/colorPrimaryDark" app:title_content_Text=" @+id/ TextView" Android :layout_width="wrap_content" Android :layout_height="wrap_content" Android :layout_height="wrap_content" Image button, set back_img attribute to display, otherwise Gone\n\n left 2: text button, set back_Text attribute to display, otherwise Gone, support to set text color and size \n\n right 1: picture button, set right_img attribute to display, otherwise Gone\n\n right 2: Text button, set right_Text property to display, otherwise Gone, Support setting text color and size \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>Copy the code
MainActivity. Used in Java
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) StatusBarUtil.translucentStatusBar(this) setContentView(R.layout.activity_main) // The return button has a default finish function, A copy, of course, also can click event / / commonTitleView setTitleBackIvClickListener {finish ()} commonTitleView. SetTitleBackTvClickListener { Toast. MakeText (this, "cancel", Toast. LENGTH_LONG), show ()} commonTitleView. SetTitleRightIvClickListener { startActivity(Intent(this,TitleActivity::class.java)) } commonTitleView.setTitleRightTvClickListener { Toast.maketext (this," share ", toast.length_long).show()}}}Copy the code
Screenshots of Mi 8

That’s it. The core code just says:

/ / set the activity immersed layout StatusBarUtil. TranslucentStatusBar (this)Copy the code

Control is also a CommonTitleView, some students may say, our product manager needs the title than this abnormal much better, may be a variety of flowery controls and some unknown SAO operation. Okay, so there’s a way to do dynamic Settings, right

Slightly more advanced usage:

Importing dependencies and setting themes are the same as above, but with two variadic methods that dynamically set controls left and right. The method name is as follows:

titleMain.addViewAttitleLeftLL(imageView, textView, checkBox)
titleMain.addViewAttitleRightLL(imageView, textView, checkBox)
Copy the code
The layout file activity_title.xml
<? The 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_content_Text=" /> </androidx.constraintlayout.widget.ConstraintLayout>Copy the code
MainActivity. Used in 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, 1 f) checkBox. SetText (" agree ") checkBox. LayoutParams = checkBoxparams checkBox. SetOnCheckedChangeListener {compoundButton, If (b){toast.maketext (this," agree ", toast.length_long).show()}else{if (b){toast.maketext (this," agree ", toast.length_long). Toast.maketext (this," agree ", toast.length_long).show()}} val button = button (this) button.text = "button" titleMain.addViewAttitleLeftLL( button, checkBox) button.setOnClickListener { finish() } } }Copy the code

Screen shot of mi 8

All right. So when I’m done with all of this I’m going to give the sample address