我在Android屏幕的下部有一个EditText,如图所示。由于一些项目限制,我只能使用绝对定位。
当点击EditText时,软键盘会隐藏EditText,如另一张图片所示。 我尝试在清单文件中添加android:windowSoftInputMode="adjustResize"(也尝试了adjustPan),它只对相对定位起作用(下面的textField_layoutparams代码已注释),但对绝对定位似乎不起作用。
甚至尝试添加了一个ScrollView(下面的代码已注释),但它也不起作用。
代码:
在绝对定位的情况下,有没有办法实现所期望的行为?
当点击EditText时,软键盘会隐藏EditText,如另一张图片所示。 我尝试在清单文件中添加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")
}
}
}
在绝对定位的情况下,有没有办法实现所期望的行为?