Android4.0 New Controls

Source: Internet
Author: User

Google introduced a number of new controls at the same time as the launch of Android4.0, with the following 5 of the most commonly used new controls in Android4.0.

1. Use of switch

Switch as the name implies, is the meaning of switching, there are two states of open and close.

When switch is in the off state:

When switch is in the open state:

How to define a switch in the definition XML
    <Switch    android:id="@+id/_switch"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:textOff="关闭"    
    • android:textOffProperty represents the text that is displayed when switch is closed
    • android:textOnProperty represents the text displayed when switch opens
    mSwitch= (Switch) findViewById(R.id._switch);    mSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {        @Override        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {            if(isChecked){                Log.i("Switch","打开Switch");            }else{                Log.i("Switch","关闭Switch");            }        }    });
2. Use of space

Space, as its name implies, means that the control occupies a certain amount of space, but does not show anything.

How to use space
    <android.support.v4.widget.Space    android:layout_width="match_parent"    android:layout_height="60dp" />  
3. Use of GridLayout

GridLayout refers to the grid layout, GridLayout is to compensate for some of the shortcomings of Tablelayout launched.
-Tablelayout cannot be aligned in both horizontal and vertical two directions, because Tablelayout inherits LinearLayout.
-Elements in tablelayout cannot span rows or columns, because tablelayout cannot explicitly indicate how many rows and columns are accounted for.

Attributes common to elements in GridLayout

    • Android:layout_row: Fixed display in the first few lines.
    • Android:layout_column: Fixed display in the first few columns
    • Android:layout_rowspan: Spanning several lines
    • Android:layout_columnspan: Spanning several columns
How to use GridLayout
 <GridLayout    android:id="@+id/grid_layout"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:columnCount="2"    android:rowCount="2">    <Button android:text="打开PopupMenu"        android:onClick="openPopupMenu"/>    <Button android:text="TextureView不旋转"        android:onClick="rotate0"/>    <Button android:text="TextureView旋转45度"        android:onClick="rotate45"/>    <Button android:text="TextureView旋转90度"        android:onClick="rotate90"/></GridLayout>  
4. Use of PopupMenu

PopupMenu, as the name implies, is a popup menu that displays a pop-up menu underneath a control.

Defining pop-up menus in XML

Create a new menu of XML files under the Menus Resource Directory

<menu xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"><item android:title="Switch" /><item android:title="Space" /><item android:title="GridLayout" /><item android:title="PopupMenu" /><item android:title="TextureView" /></menu>  
How to show PopupMenu
public void openPopupMenu(View view){    //popupMenu显示在view下面    PopupMenu popupMenu=new PopupMenu(this,view);    //从xml文件中加载菜单到popupMenu中    popupMenu.inflate(R.menu.popup_menu);    //显示  popupMenu    popupMenu.show();}
5. Use of Textureview

Textureview is a supplement to Surfaceview, it does not create special windows like Surfaceview, it creates a regular view,textureview can be set to move, rotate, animate etc.
A textureview can be used to display the content stream. Such a content stream can be a video or OpenGL scenario. The content stream can come from both the application's process and the remote process. Textureview can only be used for hardware-accelerated windows. When rendering software, Textureview will not draw anything.

How to use Textureview

Using Textureview is simple: all you have to do is get it surfacetexture. Surfacetexture can then be used to render the content.
The following example shows how to render a camera preview to Textureview:
Because the camera is used, add the appropriate permissions to the Add Androidmanifest.xml file
<uses-permission android:name="android.permission.CAMERA"/>

PublicClassMainactivityExtendsAppcompatactivityImplementsTextureview.surfacetexturelistener{Private Textureview mtexture;Private Camera Mcamera;@OverrideProtectedvoidOnCreate (Bundle savedinstancestate) {Super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Mtexture= (Textureview) Findviewbyid (R.id.texture_view);Set the surface structure listener Mtexture.setsurfacetexturelistener for Mtexture (this); }/** * Textureview Surfacetexture ready to start with */@OverridePublicvoidOnsurfacetextureavailable (Surfacetexture surface,int width,int height) {Mcamera = Camera.open ();try {Set the surface structure of the Mcamera to surface mcamera.setpreviewtexture (surface);Start Camera preview Mcamera.startpreview ();Set Mtexture transparency Mtexture.setalpha (1.0f);Set mtexture rotation Angle mtexture.setrotation (90.0f); }catch (IOException IoE) {Something bad Happened}}/** * Surfacetexture Cache size Changed */ @Override public void onsurfacetexturesizechanged (surfacetexture surface, int width, int height) {} /** * surfacetexture destroyed */  @Override public boolean Span class= "Hljs-title" >onsurfacetexturedestroyed (surfacetexture surface) {Mcamera.stoppreview (); MCamera.release ( ); return true;} /** * surfacetexture updated */ @Override public void  onsurfacetextureupdated (Surfacetexture surface) {}}         

This is defined in the XML file Textureview

    <TextureView        android:id="@+id/texture_view"android:layout_width="match_parent"android:layout_height="match_parent">
    </textureview>

Note: The Textureview setting rotates 90 degrees is our normal view.

Android4.0 New Controls

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.