Original address: http://blog.csdn.net/lavor_zl/article/details/51261380
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
How to listen for switch off, open the change of both states
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"/>
Public class mainactivity extends appcompatactivity implements Textureview. Surfacetexturelistener {PrivateTextureview mtexture;PrivateCamera Mcamera;@Overrideprotected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main); Mtexture= (Textureview) Findviewbyid (R.id.texture_view);//Set surface structure listener for mtextureMtexture.setsurfacetexturelistener ( This); }/** * Textureview surfacetexture ready to start with */@Override Public void onsurfacetextureavailable(Surfacetexture surface,intWidthintHeight) {Mcamera = Camera.open ();Try{//Set the surface structure of the Mcamera to surfaceMcamera.setpreviewtexture (surface);//Start Camera previewMcamera.startpreview ();//Set mtexture transparencyMtexture.setalpha (1.0f);//Set mtexture rotation angleMtexture.setrotation (90.0f); }Catch(IOException IoE) {//Something bad happened} }/** * surfacetexture Cache size changed */@Override Public void onsurfacetexturesizechanged(Surfacetexture surface,intWidthintHeight) {}/** * surfacetexture destroyed * /@Override Public Boolean 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.
6. An activity master Android4.0 new controls
To facilitate learning, I put these 5 new controls into one activity for use.
- Program Original Interface Explained
- Turn on the switch and click "Open PopupMenu" to eject PopupMenu
- The following are Textureview rotation 0 degrees (that is, do not rotate), rotated 45 degrees, rotated 90 degrees three cases of explanation
This program source code download a activity Master Android4.0 new control
From for notes (Wiz)
An Activity Master Android4.0 new control (GO)