How would you create a popover view in Android, like Facebook Comments?

后端 未结 5 1401
渐次进展
渐次进展 2021-01-29 23:06

I was wondering if anyone knows how to create a Facebook-like popover view like in the Facebook Android app for comments.

This is what I mean:

5条回答
  •  执念已碎
    2021-01-29 23:44

    Edit:

    Actually, even though I used a DialogFragment, I'm quite certain their popup does not use a DialogFragment (or even a Dialog at all!). The reason for this is the resizing feature. If that is something you want, then you can't use a DialogFragment. You would have to just add a new view to your layout. It looks like facebook also has another view that sits between your wall and the fake popup that is slightly translucent and listens for clicks in order to dismiss the view. Something like this would take some actual effort and time to build, so I won't make this one for you. Let me know if you have any questions about it though, I can probably guide you to the solution you are after.

    Original:

    I wrote the popup for you:

    public class MyActivity extends Activity {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            if (savedInstanceState == null) {
                F1.newInstance().show(getFragmentManager(), null);
            }
        }
    
        public static class F1 extends DialogFragment {
    
            public static F1 newInstance() {
                F1 f1 = new F1();
                f1.setStyle(DialogFragment.STYLE_NO_FRAME, android.R.style.Theme_DeviceDefault_Dialog);
                return f1;
            }
    
            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    
                // Remove the default background
                getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    
                // Inflate the new view with margins and background
                View v = inflater.inflate(R.layout.popup_layout, container, false);
    
                // Set up a click listener to dismiss the popup if they click outside
                // of the background view
                v.findViewById(R.id.popup_root).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dismiss();
                    }
                });
    
                return v;
            }
        }
    }
    

    popup_layout.xml:

    
    
    
    
        
    
            
    
        
    
    
    

    And dialog_background.xml (goes into res/drawable):

    
    
        
        
        
    
    

    And it looks like this:

    enter image description here

    Just add your view content and you're good to go!

提交回复
热议问题