Simple use case for layout method

Source: Internet
Author: User

Layout represents the location of a view in the parent view, such as
ViewGroup parent = new ViewGroup ();
View child = new View ();
Parent.addview (Child)
Child.layout (L,T,R,B);
PS: Initializing objects with new is just to make it easier to explain the situation
At this point, child view,child.layout (L,T,R,B), which is the parent's son, illustrates the coordinate position that children placed in the parent, and the coordinate origin (0,0) is the upper-left corner of the parent.

Where (L,t) represents the upper-left corner of the position, (R,B) represents the lower-right corner of the position.

Use case, this page: The page has more than one view to get the focus and give a focus frame to the view that gets to the focus.


At the beginning of the implementation of this page, the use of the Transduction method, that is, each view of the focus frame cut a graph, placed in

<selector xmlns:android= "Http://schemas.android.com/apk/res/android" >    <!--focus_img: Focus Frame Picture--    <item android:state_focused= "true" android:drawable= "@drawable/focused_img"/>    <item android: State_selected= "true" android:drawable= "@drawable/focused_img"/>    <item android:state_pressed= "true" android:drawable= "@drawable/focused_img" ></item>    <item android:drawable= "@drawable/unfocused_ IMG "/></selector>

This configuration file allows one to set a view of the background. But there's a problem with this: there are several different sizes of view that require a focus frame effect on this page. This means that you want the artist to cut different focus frames for different view. To know that this is only an application interface, there are other pages also need to get the focus frame, then you have to let the artwork cut. In this case, you will have to configure a selector for each focus frame for this repetitive work.
Here's another way, it's time for layout to come out:

Ideas:
1) provide a focus frame for a. 9 picture
2) Provide a imageview, this ImageView src is. 9 picture, assuming that the ImageView variable is named Focusimgview
3) The view setting of True for page focusable Foncuschangelistener, if a view takes focus, gets the position (x, y) and its width (width,height) of the upper-left corner of the view. Then set
Focusimgview.layout (X,y,x+width,y+height) (because there is no direct method in Android to get the position of the lower right corner of a view, it needs to be calculated by width and height)

The basic code is as follows:

/** Focus Frame View**/public class Focusview extends viewgroup{/** Focus box **/private ImageView focusimgview;/** page gets focus view**/ Private View focusview;/* focus. 9 pic */private drawable mviewfocusdrawable;public focusview (Context context,drawable mviewfocusdrawable) {super (context); init (mviewfocusdrawable);} private void init (drawable mviewfocusdrawable) {Focusimgview = new ImageView (GetContext ()); Viewgroup.layoutparams params = new Viewgroup.layoutparams (ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); AddView (Focusimgview, params); Focusimgview.setbackgrounddrawable ( mviewfocusdrawable);} /** * Set Focus picture * @param d */public void setfocusdrawable (drawable mviewfocusdrawable) {focusimgview.setbackgrounddrawable (mviewfocusdrawable);} /** @focusView get focus view*/public void Showfocus (View focusview) {int[] location = new INT[2]; Focusview.getlocationinwindow (location); int x = Location[0];int y = location[1];int width = focusview.getwidth (); int        Height = focusview.getheight (); This is the key step foCusimgview.layout (x, y,x+ width,y + height);} @Overrideprotected void OnLayout (Boolean changed, int l, int t, int r, int b) {}}
This is called in the Onfoucuschange method: @Overridepublic void Onfocuschange (View view, Boolean hasfocus) {int viewId = View.getid (); Switch (viewId) {case R.id.demo_btn:if (hasfocus) {mfocusview.showfocus (view);} Break;}}

Such a simple. 9 picture will be able to meet the application of the page

Simple use case for layout method

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.