How to create an overlay view within an activity

前端 未结 1 997
夕颜
夕颜 2021-02-04 19:26

I have a requirement where i have an activity which shows list of items like facebook feeds and when clicking on a button from one of the list item a dialog has to popup which w

相关标签:
1条回答
  • 2021-02-04 19:57

    You don't actually have to use a Dialog. I think dialogs are more appropriate when you want to show simple views or just an alert/confirmation to the user (normally done with an AlertDialog).

    For your situation I guess the best approach would be to have a FrameLayout on your Activity, sibling of your main layout element, and add a Fragment to it when you want to show a popup like that over your main Activity's layout. As long as you put the fragment's view after your activity's root layout element, the fragment will be displayed on top of your main layout, as an overlay. e.g.:

    <merge xmlns:android="http://schemas.android.com/apk/res/android">
        <LinearLayout android:layout_width="match_parent"
                      android:layout_height="match_parent"
                      android:orientation="vertical">
            <!-- Activity's main layout here -->
        </LinearLayout>
    
        <FrameLayout android:id="@+id/overlay_fragment_container"
                     android:layout_width="match_parent"
                     android:layout_height="match_parent"/>
    </merge>
    

    and then on your Activity when you want to display the fragment you do:

    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction()
            .add(R.id.overlay_fragment_container, yourFragment)
            .commit();
    

    Hope it helps :) Luck!

    0 讨论(0)
提交回复
热议问题