在相对布局中,windowSoftInputMode="adjustResize"似乎在绝对定位方面无效。

5
我在Android屏幕的下部有一个EditText,如图所示。由于一些项目限制,我只能使用绝对定位。
当点击EditText时,软键盘会隐藏EditText,如另一张图片所示。

enter image description here enter image description here

我尝试在清单文件中添加android:windowSoftInputMode="adjustResize"(也尝试了adjustPan),它只对相对定位起作用(下面的textField_layoutparams代码已注释),但对绝对定位似乎不起作用。
甚至尝试添加了一个ScrollView(下面的代码已注释),但它也不起作用。
代码:
class MainActivity : AppCompatActivity() {

private lateinit var binding: ActivityMainBinding

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

        val scrollView: ScrollView
        val relativeLayout: RelativeLayout
        val textField: EditText

    /*scrollView = ScrollView (this)
    scrollView.layoutParams = RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.MATCH_PARENT,
        RelativeLayout.LayoutParams.MATCH_PARENT
    )*/
    //scrollView.isFillViewport = true

    //setContentView(scrollView)

    relativeLayout = RelativeLayout(this)
    relativeLayout.layoutParams = RelativeLayout.LayoutParams (
        RelativeLayout.LayoutParams.MATCH_PARENT,
        RelativeLayout.LayoutParams.MATCH_PARENT
    )
    relativeLayout.setBackgroundColor(Color.parseColor("#0000FF"))

    //scrollView.addView(relativeLayout)
    setContentView (relativeLayout)

    textField = EditText(this)

    val textField_layoutparams: LayoutParams
    textField_layoutparams = RelativeLayout.LayoutParams (
        400,
        150
    )
    textField.setBackgroundColor(Color.parseColor("#FFFFFF"))

    //textField_layoutparams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE)
    //textField_layoutparams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE)
    //textField_layoutparams.setMargins(0, 0, 0, 200)
    textField.layoutParams = textField_layoutparams
    textField.hint = "Enter Text"
    textField.x = 500F
    textField.y = 2200F

    relativeLayout.addView(textField)
}

/**
 * A native method that is implemented by the 'scrollviewpoc' native library,
 * which is packaged with this application.
 */
external fun stringFromJNI(): String

companion object {
    // Used to load the 'scrollviewpoc' library on application startup.
    init {
        System.loadLibrary("scrollviewpoc")
    }
}
}

在绝对定位的情况下,有没有办法实现所期望的行为?
1个回答

0
我认为这个问题是在你尝试从Kotlin端设置EditText的x和y时产生的。
在Manifest中,属性android:windowSoftInputMode="adjustResize"适用于所有Android设备,但在你的情况下,我注意到当你以编程方式设置X和Y时有所不同。
你可以尝试这个方法,希望对你有用。
override fun onCreate(){
    // ...
    relativeLayout.addView(textField) // your last line add bellow of that code

    val rootView = findViewById<View>(android.R.id.content)

    rootView.viewTreeObserver.addOnPreDrawListener(object : ViewTreeObserver.OnPreDrawListener {
        override fun onPreDraw(): Boolean {
            val heightDiff = rootView.height - relativeLayout.height

            if (heightDiff > 100) { // Adjust this threshold according to your needs
                // Soft keyboard is shown
                val newTopMargin = 2200 - heightDiff
                textField_layoutparams.topMargin = newTopMargin
                textField.layoutParams = textField_layoutparams
            } else {
                // Soft keyboard is hidden
                textField_layoutparams.topMargin = 2200
                textField.layoutParams = textField_layoutparams
            }

            return true
        }
    })
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接