Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adjust login layout, fix #40 #43

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.LinearLayout
import com.quxianggif.core.GifFun
import kotlinx.android.synthetic.main.activity_login.view.*
import kotlinx.android.synthetic.main.activity_login.view.loginBgWallLayout
import kotlinx.android.synthetic.main.activity_login.view.loginInputElements
import kotlinx.android.synthetic.main.activity_login.view.loginLayoutBottom
import kotlinx.android.synthetic.main.activity_login.view.loginLayoutTop

/**
* 自定义登录界面Layout,监听布局高度的变化,如果高宽比小于4:3说明此时键盘弹出,应改变布局的比例结果以保证所有元素
Expand All @@ -35,30 +37,54 @@ class LoginLayout(context: Context, attributes: AttributeSet) : LinearLayout(con

var keyboardShowed = false

var loginInputElementsPreferHeight = -1

override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
super.onLayout(changed, left, top, right, bottom)
// 键盘弹出后 loginLayoutBottom 的最小高度为
if (loginInputElementsPreferHeight == -1) loginInputElementsPreferHeight = loginInputElements.height
if (changed) {
val width = right - left
val height = bottom - top
if (height.toFloat() / width.toFloat() < 4f / 3f) { // 如果高宽比小于4:3说明此时键盘弹出
post {
loginBgWallLayout.visibility = View.INVISIBLE
val params = loginLayoutTop.layoutParams as LayoutParams
params.weight = 1.5f
keyboardShowed = true
loginLayoutTop.requestLayout()
// 按照 1.5 / 4 比例计算 loginLayoutBottom 的高度
val loginInputElementsAvailableHeight = (height * (4 / 5.5)).toInt()
// 如 loginLayoutBottom 可以承载 loginInputElements 的高度则直接按照比例分配
// 反之则优先分配 loginLayoutBottom 高度, 剩余高度设置给 loginLayoutTop
if (loginInputElementsAvailableHeight >= loginInputElementsPreferHeight){
post {
loginBgWallLayout.visibility = View.INVISIBLE
processLayoutParams(loginLayoutTop, 0, 1.5f)
processLayoutParams(loginLayoutBottom, 0, 4f)
keyboardShowed = true
requestLayout()
}
} else {
post {
loginBgWallLayout.visibility = View.INVISIBLE
processLayoutParams(loginLayoutTop, height - loginInputElementsPreferHeight, 0f)
processLayoutParams(loginLayoutBottom, loginInputElementsPreferHeight, 0f)
keyboardShowed = true
requestLayout()
}
}
} else {
if (keyboardShowed) {
post {
loginBgWallLayout.visibility = View.VISIBLE
val params = loginLayoutTop.layoutParams as LayoutParams
params.weight = 6f
processLayoutParams(loginLayoutTop, 0, 6f)
processLayoutParams(loginLayoutBottom, 0, 4f)
loginLayoutTop.requestLayout()
}
}
}
}
}

private fun processLayoutParams(view: View, height: Int, weight: Float){
val params = view.layoutParams as LayoutParams
params.height = height
params.weight = weight
}

}
8 changes: 6 additions & 2 deletions opensource/src/main/res/layout/activity_login.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@
</LinearLayout>

<ImageView
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxWidth="70dp"
android:maxHeight="70dp"
android:layout_marginTop="16dp"
android:adjustViewBounds="true"
android:layout_centerInParent="true"
android:src="@drawable/logo_reverse"
android:transitionName="@string/transition_logo_splash" />
Expand Down