LoginSignup
0
0

More than 3 years have passed since last update.

AndroidのFragmentでOverlayViewを表示する

Last updated at Posted at 2019-05-20

Solution

res/layout/partial_overlay.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/overlay_screen_bg">

    <!-- クリックイベントをセットするためのView -->
    <View
        android:id="@+id/view_bg"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

...
</androidx.constraintlayout.widget.ConstraintLayout>
HogeFragment.kt
class HogeFragment : Fragment() {

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        showOverlayView()
    }

    private fun showOverlayView() {
        // activityのrootを取得
        val rootLayout = requireActivity().findViewById(android.R.id.content) as FrameLayout
        // overlayViewを表示
        val overlayView = View.inflate(requireActivity(), R.layout.partial_overlay, rootLayout)

        // view_bgをタップしたら非表示にする
        overlayView.view_bg.setOnClickListener {
            rootLayout.removeViewAt(rootLayout.childCount - 1)
        }
    }
}

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0