【问题标题】:Popup window in fragment in kotlinkotlin片段中的弹出窗口
【发布时间】:2021-11-28 07:43:34
【问题描述】:

我需要弹出窗口来显示我的用户选项 例如,我想添加一个长按按钮,然后当单击该按钮时,显示 一个带有透明背景的弹出窗口,当在外面点击时关闭弹出窗口

exactly like this

我尝试了很多方法,但没有一个对我有用。

我也不知道如何使用对话窗口。

无论使用哪个,dilog 片段或弹出窗口,但我需要我的窗口to be like this(on the top is user name which has given from data base and then options)

这里是我应该在哪里放置弹出窗口:

val mUserAdapter = UserAdapter()
        mUserAdapter.setOnclickListener(AdapterListener({
            if (it != 0L)
                this.findNavController().navigate(
                    UserListFragmentDirections.actionUserListFragmentToIncreaseMoneyFragment(it)
                )
            Log.d("TAG", "navTeat $it ")
        }, {
            deleteDialog(it)
        }
        ) {
            Toast.makeText(activity, "Long", Toast.LENGTH_SHORT).show() //Here instead of Toast, I need POPUP WINDOW

        })

谢谢:)

【问题讨论】:

  • 你在代码中做了什么。
  • 你是说那张图片吧?实际上它是另一个应用程序,我的意思是它不是我的应用程序。我发布这张图片是为了向其他人展示我的弹出窗口应该是怎样的
  • 我想看看你的代码
  • 因为这些代码不起作用,我真的很生气,所以我删除了所有代码,然后将该项目推送到 git lab,但如果你愿意,我可以分享项目的链接,这是其中之一我尝试过但仍然没有工作的souses youtube.com/watch?v=fn5OlqQuOCk

标签: android kotlin android-alertdialog android-dialogfragment popupwindow


【解决方案1】:

1.对话框片段

假设您使用的是导航组件,请在 navGraph 中使用

标签添加 dialogFragment,

<dialog
    android:id="@+id/navigation_dialog_fragment"
    android:name="com.app.example.ui.dialogFragment"
    tools:layout="@layout/dialogFragment"/>

覆盖对话框片段的onCreate() 以使背景半透明,

class mDialogFrag: DialogFragment(R.layout.dialog_fragment) {

   override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
         setStyle(STYLE_NO_FRAME, R.style.DialogTheme)

         //populate UI with data from DB

对话主题:

<style name="AppDialogTheme" parent="Theme.AppCompat.Light">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">#40000000</item><!--dialog background-->
    <item name="android:backgroundDimEnabled">true</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowFullscreen">true</item>
</style>

2.警报对话框

在您的目标片段中,

lateinit var mAlert : AlertDialog
//inflate custom layout
val alertBinding = AlertBinding.inflate(LayoutInflater.from(requireContext()))
alertBinding?.apply{
  //populate UI with data from DB
}
val builder = AlertDialog.Builder(requireContext())
mAlert = builder.setView(alertBinding.root)
             .setCancelable(false)
             .create()
mAlert.window?.setBackgroundDrawable(
     ContextCompat.getDrawable(requireContext(),R.drawable.bg_card) //custom dialog background 
)
mAlert.show()

这两种方法都使用自定义布局来满足您的要求。 但 DialogFragment 用于更复杂的 UI(即动态地提供对 UI 的更多控制)。 而 AlertDialog 可用于更简单的 UI,例如您的。

或者您可以使用称为传统单选列表AlertDialog 的默认列表,例如here,这样更简单。

【讨论】:

  • 嗨,@Ezhilan 我无法理解 AlertBinding。这是什么??
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多