Android DialogFragment 相关代码段

设置软键盘弹出,自动上移

1
2
3
4
5
6
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
...
// 设置有软键盘时,窗口自动上移
dialog?.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)
...
}

设置全屏显示

1
2
3
4
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setStyle(STYLE_NORMAL, R.style.Dialog_FullScreen)
}
1
2
3
4
5
<style name="Dialog.FullScreen" parent="Theme.AppCompat.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@color/transparent</item>
<item name="android:windowIsFloating">false</item>
</style>

子Fragment

1
2
3
4
5
private fun replace(fragment: androidx.fragment.app.Fragment) {
val transaction = childFragmentManager.beginTransaction()
transaction.replace(R.id.fragment, fragment)
transaction.commit()
}
阅读更多

Android动画相关代码段

ValueAnimator 实现TextView打字机效果

1
2
3
4
5
6
7
8
9
10
11
fun TextView.startTypeAnimation(duration: Long = 300) {
val originText = this.text
val valueAnimator = ValueAnimator.ofInt(0, originText.length)
valueAnimator.duration = duration
valueAnimator.interpolator = LinearInterpolator()
valueAnimator.addUpdateListener { animation ->
val length = animation.animatedValue as Int
this.text = originText.subSequence(0, length)
}
valueAnimator.start()
}

SpringInterpolator 简易的弹性插值器

1
2
3
4
5
6
7
8
/**
* 弹性插值器
*/
class SpringInterpolator(private val factor: Float) : Interpolator {
override fun getInterpolation(input: Float): Float {
return (2.0.pow((-10 * input).toDouble()) * sin((input - factor / 4) * (2 * Math.PI) / factor) + 1).toFloat()
}
}
阅读更多