我正在try 使用设置为View.GONE.的LinearLayout创建一些下拉菜单.当单击按钮时,可见性将变为可见,内容将垂直显示在按钮下方.我希望发生的是,垂直视图将绘制在按钮下方的任何视图上.现在发生的是按钮下方的视图并被按下.
有没有办法实现这一点?
我正在try 使用设置为View.GONE.的LinearLayout创建一些下拉菜单.当单击按钮时,可见性将变为可见,内容将垂直显示在按钮下方.我希望发生的是,垂直视图将绘制在按钮下方的任何视图上.现在发生的是按钮下方的视图并被按下.
有没有办法实现这一点?
要实现在按钮下方的任何视图上绘制垂直视图的所需行为,可以使用PopupWindow
in Kotlin.下面是一个简单的例子:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val button = findViewById<Button>(R.id.button)
val popupView: View = LayoutInflater.from(this).inflate(R.layout.popup_layout, null)
val popupWindow = PopupWindow(
popupView,
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT,
true
)
button.setOnClickListener {
if (popupWindow.isShowing) {
popupWindow.dismiss()
} else {
// Show popup at the bottom of the button
popupWindow.showAsDropDown(button)
}
}
}
}