Android Foundation VII: UI Development

Source: Internet
Author: User

  • Programming elements that need to be used
    • Views: view, the base class for visual elements, all components, views, etc. are derived from view
    • View group: ViewGroup, which is an extension of view, can include multiple sub-views
    • Fragment: Similar to the previous activity fragment, has a separate layout, but needs to be bound to the activity
    • Activity: Basic display unit for displaying the interface
  • Layout
    • The layout manager is an viewgroup-based extension that controls the location of child controls and is capable of nesting
    • A layout is usually defined by a separate layout file, which separates the code from the resource and facilitates management.
    • There are many layout classes available in the
    • Android SDK that you can use to implement layouts, or you can modify these classes to create your own layouts
      • framelayout
        • simplest layout manager
        • Based on upper left corner layout
        • can change position by gravity
        • Overlay layout
        • margin Property definition location not valid
      • linearlayout
        • Linear layout
        • must define the layout direction: oritation
        • One view component can be a single row or column
        • You can define a location by margin or gravity
      • relativelayout
        • relative layout
        • The most flexible layout that allows layout to be laid out relative to screen boundary distance
        • default relative top,left layout
        • Of course sub-layouts can also be positioned relative to other components
      • GridLayout
        • Lift rectangular grid layout
        • Android4.0 later available
        • Simplified nesting of layouts
    • Defining layouts
      • Creating an XML layout file in the Layout resource folder to enable editing of layouts
      • The general root element is LinearLayout
      • Where the common attributes and values
        • Wrap_contant: Used to set the width or height value, which is calculated based on the content
        • Match_parent: Used to set the width or height value as the size of the parent element
        • Fill_parent: Used to set the width or height value, which represents the size that is allowed to be populated with the parent element, basically the same as Match_parent
        • Gravity: The position of the inner element
        • Layout_gravity: The position of itself relative to the parent element, Note that it does not work in Framelayout
        • Textallcaps: letter Case Display Flag
        • Hint: Similar to placeholder, watermark hint
        • Entries:adapter adapter padding for populating data such as list components
    • Code creation layout (although strongly recommended not to use)
        • Each layout element has a corresponding class that enables the code to construct the layout
        • instance code
        •  linearlayout linearlayout=new  linearlayout (myactivity. This  ); TextView TV  =new  TextView (myactivity. This  ); Linearlayout.layoutparams layoutparams  =new  Linearlayout.layoutparams
        • use Findviewbyid to get child elements in the Contview layout file
        • layout file XML to view
          • Layoutinflator class is a tool class specifically designed to resolve layout files into view instances
          • Get Layoutinflator instances Method
            • activity in Getlayoutinflator ()
            • internal static method Layoutinflator.from (context)
            • System SERVICE Acquisition: (layoutinflator) Getsystemservice (context.layout_inflator_service);
    • layout nesting
      • padding layout is very expensive, complex interfaces require a large number of layouts to nest
      • Note redundancy to circumvent UI layouts
      • reduce unnecessary parent elements, such as framelayout child elements with only one Linearla Yout, then what's the meaning of this framelayout, and so on.
      • Layout Fill merge and include use
        • include is the use of inserting one layout into another layout
        • child layout The parent element is set to Framelayout, and the layout is more resource-intensive. You can use merge instead of the root element, and the merge will automatically delete the
      • when it is populated. Do not use too many view, too many view fills need to consume a lot of time and resources, affect the program response speed, A single non-centered view cannot be more than 80, the less the better
      • if there is a large number of view, then use Viewstub to control the view
      • viewstub
        • viewstub that are temporarily not required to display Create a view layout that you don't want to display temporarily, you can create it dynamically, or you can configure the layout file statically
        • to display the
        • by inflate or setvisibility of the viewstub instance when it needs to be displayed The difference between inflate and setvisibility
          • inflate can only be populated once, used for view fills, is empty after padding, cannot be called the inflate method for the second time, or throws an exception
          • Setvisi Bility can display the hidden control viewstub multiple times, so it is recommended that the elements in the setvisibility
        • viewstub be populated before they are fetched through Findviewbyid, otherwise they will throw an exception
    • Split layout fragment
      • fragment is a next-generation fragment component that replaces the initial activity fragment design, which is lighter than activity and useful for its full life cycle
      • Fragment can achieve different screen size devices create a flexible UI
      • Each fragment is a separate module, and is bound to the activity, the relationship is many-to-many
      • Fragment occurs after Android3.0, before 3.0 if you use
        • to create a
          • that must inherit from the Fragmentactivity
          • fragment A class that inherits from Fragment, which is after Android3.0;
          • Populating the desired layout in Oncreateview
          • create fragment own layout file resources, you can create resources of different screen sizes, dynamically determine the resources that are populated in Oncreateview
        • fragment life cycle
          • Onattach: Bind fragment to Activity
          • onCreate: Create
          • Oncreatev Iew: Fill View
          • onactivitycreated:activity filled
          • onresume
          • onPause
          • Onsavestatein Stance
          • onStop
          • ondestoryview: Empty view
          • ondestory: Destroy
          • Ondetach: Unbind
          • fragment communication with activity

Android Foundation VII: UI Development

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: 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.