Application of FlowLayout in Popupwindow

Source: Internet
Author: User

Pop-up box is the style of the requirement. The contents of the inside can be arbitrarily added and censored. The right corner points to the target view. To achieve both of these requirements, you need to use the common hot tabs for streaming layouts and to set the XY of the popup box. There is also an invisible demand, the width of the popup box, only the maximum and minimum values are known. The maximum is the screen width, the minimum is the width of the caption submit button


To implement the above style I applied an open source project on Popupwindow and GitHub android-flowlayout https://github.com/ApmeM/android-flowlayout, FlowLayout in the calculation of the width of the place there are two places do not meet the needs, so I made the changes, https://github.com/langzuxiaozi/android-flowlayout.

First look at the Popupwindow layout file.

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/    Android "Android:layout_width=" Match_parent "android:layout_height=" wrap_content "android:orientation=" vertical "        android:paddingleft= "16DP" android:paddingright= "16DP" > <viewstub android:id= "@+id/img_top_layout" Android:layout_width= "Match_parent" android:layout_height= "wrap_content" android:layout= "@layout/corner_lay        Out "/> <linearlayout android:id=" @+id/title_layout "android:layout_width=" Match_parent " android:layout_height= "40DP" android:background= "@drawable/pop_window_top_item_background" Android:orien tation= "Horizontal" android:gravity= "center_vertical" > <textview android:id= "@+id/t Itle_1 "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "Android:           layout_marginleft= "13DP" android:text= "Nasty Reason" android:textcolor= "@color/white" android:textsize= "14sp"/> <text View android:id= "@+id/title_2" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "(Multiple selection)" android:textcolor= "@color/white_alpha60" Android:text            Size= "14sp"/> <view android:layout_width= "0DP" android:layout_height= "0DP" android:layout_weight= "1"/> <button android:id= "@+id/commit" android:layout_width= "48d P "android:layout_height=" 24DP "android:layout_marginleft=" 8DP "Android:layout_marginrigh t= "8DP" android:background= "@drawable/commit_btn_background_selector" android:text= "commit" a Ndroid:textcolor= "@color/white" android:textsize= "12sp"/> </LinearLayout> <com.example.wolf. Popupwindow_flowlayout.fLowlayout. FlowLayout xmlns:f= "Http://schemas.android.com/apk/res-auto" android:id= "@+id/content_flowlayout" and Roid:layout_width= "Match_parent" android:layout_height= "wrap_content" android:background= "@drawable/pop_wind Ow_bottom_item_background "android:paddingbottom=" 8dip "android:paddingtop=" 13dip "Android:paddinglef t= "9dip" android:paddingright= "9dip" android:orientation= "Horizontal" > </com.example.wolf.pop upwindow_flowlayout.flowlayout.flowlayout> <viewstub android:id= "@+id/img_bottom_layout" Android:layout_w Idth= "Match_parent" android:layout_height= "wrap_content" android:layout= "@layout/corner_layout"/> </LinearLayout>

Title_layout is the title submit button that layer

Content_flowlayout is a popular label that layer

Img_bottom_layout Img_top_layout These are the horns of Popupwindeow, and only one layout will be displayed. is the result of the img_top_layout display.

The idea of calculating the width is to calculate the wide content_flowlayout width of the title_layout and take the maximum value. Then compare it to the screen width and take the minimum value. The result of the final result is the width of the popupwindeow.

Set the width of the Popupwindeow, and then calculate the height of the Popupwindeow.

When you get the XY coordinate of the target view (the upper-left corner of the view), the position to display is calculated based on the width of the popupwindeow.

The code that displays Popupwindeow is:

private void Showpopupwindow (view parent) {//a custom layout as displayed content View Contentview = Layoutinflater.from (This)        . Inflate (R.layout.pop_window, NULL);        FlowLayout = (FlowLayout) Contentview.findviewbyid (r.id.content_flowlayout);        String [] stings = {"Big liar", "dislike", "adult scum", "Love", "very rubbish", "Big Bad", "Big fool"};        int margin = dip2px (this, 4); for (int i=0;i<stings.length;i++) {checkbox btn = (checkbox) Layoutinflater.from (this). Inflate (R.layout.che            Ckbox, Null,false);            Btn.settext (Stings[i]); Flowlayout.layoutparams LP = new Flowlayout.layoutparams (LinearLayout.LayoutParams.WRAP_CONTENT,            LinearLayout.LayoutParams.WRAP_CONTENT);            Lp.setmargins (margin, margin, margin, margin);        Flowlayout.addview (BTN,LP); }//Gets the maximum width of the screen windowmanager Mwindowmanager = (WindowManager) This.getsystemservice (this.        Window_service);       int screenwidth = Mwindowmanager.getdefaultdisplay (). GetWidth (); int h = View.MeasureSpec.makeMeasureSpec (0,view.measurespec.unspecified);        int w = View.MeasureSpec.makeMeasureSpec (screenwidth,view.measurespec.at_most);        int mwidth=0;        Get FlowLayout wide Flowlayout.measure (w, h);        Mwidth = Flowlayout.getmeasuredwidth ();        ViewGroup Vgroup = (viewgroup) Contentview.findviewbyid (r.id.title_layout);        W = View.MeasureSpec.makeMeasureSpec (0,view.measurespec.unspecified);        Gets the width of the title_layout view, and compares it with the flowlayout width, taking the maximum value vgroup.measure (w, h);        Mwidth = Math.max (Mwidth,vgroup.getmeasuredwidth ()); Gets the maximum width of the child view, plus the left and right padding of the parent view, to get the true width of the parent View Mwidth+=contentview.getpaddingleft () +contentview.getpaddingright ()        ;        If the parent view is wider than the screen, take the width of the screen to the final popupwindeow width mwidth = math.min (mwidth,screenwidth); Final Popupwindow Popupwindow = new Popupwindow (Contentview, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup .        Layoutparams.wrap_content, True); Popupwindow.setwIdth (Mwidth);        Popupwindow.settouchable (TRUE); Popupwindow.settouchinterceptor (New View.ontouchlistener () {@Override public boolean onTouch (View V                , Motionevent event) {return false;        If this returns true, the touch event will be intercepted//intercepted after the Popupwindow ontouchevent is not called, so that clicking on the external area cannot be dismiss}); If you do not set the background of the Popupwindow, either clicking the external area or the back button will not dismiss the frame//I think this is a bug in the API Popupwindow.setbackgrounddrawa        BLE (new bitmapdrawable ());        Gets the xy coordinate of the target view int[] location = new INT[2];        Parent.getlocationonscreen (location);        int parentxpos = location[0];        int parentypos = location[1];        The position (xy coordinate) W = View.MeasureSpec.makeMeasureSpec (screenwidth,view.measurespec.at_most) shown by the Popupwindow's width and height calculation;        Popupwindow.getcontentview (). Measure (W, h);        int yPos = Parentypos-popupwindow.getcontentview (). Getmeasuredheight (); int xPos = parentxpos-mwidth + popupwiNdow.getcontentview (). Getpaddingright () +dip2px (this, ten);//+parent.getwidth ();        Here you get all the areas except the system's own display area, which is all areas except the top one showing the status bar of the charge, rect framerect = new rect ();        This.getwindow (). Getdecorview (). Getwindowvisibledisplayframe (Framerect);        Determine whether the Popupwindow is displayed on the top or bottom of the target view, judging by the status bar view inflated = null; Framerect.top here can get the height of the status bar, that is, the top one shows the power, signal, etc. if ((framerect.top) >ypos) {YPos = parentypos+parent.ge            Theight ();            Viewstub stub = (viewstub) Contentview.findviewbyid (r.id.img_top_layout);            inflated = Stub.inflate ();            Set the display angle up icon ImageView IV = (ImageView) Inflated.findviewbyid (r.id.img);        Iv.setimageresource (R.drawable.pop_window_corner_top);            }else{viewstub stub = (viewstub) Contentview.findviewbyid (r.id.img_bottom_layout);            inflated = Stub.inflate (); Because the viewstub is used, the height of the computed Popupwindow is not calculated when the picture is high inflated.measure (w,h);        YPos-=inflated.getmeasuredheight ();    }//Set parameters and then show popupwindow.showatlocation (parent, gravity.no_gravity, XPos, YPos); }


Source

http://download.csdn.net/detail/langzxz/8894973







Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Application of FlowLayout in Popupwindow

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.