谷歌在推出Android4.0的同时推出了一些新控件,Android4.0中最常用的新控件有下面5种。 

1. Switch的使用


Switch顾名思义,就是开关的意思,有开和关两种状态。

当Switch处于关闭状态时: 
 
当Switch处于打开状态时: 

怎么在定义xml中定义Switch


    <Switch
android:id="@+id/_switch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textOff="关闭"
android:textOn="打开" />
  • android:textOff属性表示Switch关闭时显示的文本
  • android:textOn属性表示Switch打开时显示的文本

    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. Space的使用


Space顾名思义是空间的意思,表示该控件占据一定的空间,但是却不显示任何东西。

怎么使用Space


    <android.support.v4.widget.Space
android:layout_width="match_parent"
android:layout_height="60dp" />

3. GridLayout的使用


GridLayout是指网格布局,GridLayout是为了弥补TableLayout的一些不足而推出来的。 
- TableLayout不能同时在水平和垂直两个方向上对齐,因为TableLayout继承LinearLayout。 
- TableLayout中的元素不能跨行或者跨列,因为TableLayout不能明确指出占多少行和多少列。

GridLayout中元素常用的属性

  • android:layout_row : 固定显示在第几行。
  • android:layout_column : 固定显示在第几列
  • android:layout_rowSpan : 跨几行
  • android:layout_columnSpan: 跨几列

怎么使用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. PopupMenu的使用


PopupMenu顾名思义是弹出菜单,它可以在一个控件的下面显示弹出菜单。

在xml中定义弹出菜单

在menu资源目录下面新建一个菜单的xml文件

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

怎么显示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. TextureView的使用


TextureView是SurfaceView的补充,它不像SurfaceView一样创建特殊的窗口,它创建一个常规的View,TextureView可以设置移动,旋转,动画等。 
一个Textureview可以用来显示内容流。这样的内容流可以是视频或OpenGL场景。内容流可以来自于应用程序的进程以及远程进程。Textureview只能用于硬件加速的窗口。当渲染软件,Textureview什么都不会画。

怎么使用TextureView

使用Textureview很简单:你需要做的就是得到它的SurfaceTexture。然后,SurfaceTexture可用于呈现内容。 
下面的示例演示如何渲染相机预览到Textureview: 
因为使用了相机,所以要在添加AndroidManifest.xml 文件中添加对应的权限 
<uses-permission android:name="android.permission.CAMERA"/>

public class MainActivity extends AppCompatActivity implements TextureView.SurfaceTextureListener{private TextureView mTexture;
private Camera mCamera;
@Overrideprotected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTexture= (TextureView) findViewById(R.id.texture_view);
//为mTexture设置表面结构监听器
mTexture.setSurfaceTextureListener(this); } /**
* TextureView的SurfaceTexture准备开始用
*/@Overridepublic void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
mCamera = Camera.open();
try {
//设置mCamera的表面结构为surface
mCamera.setPreviewTexture(surface);
//启动相机预览
mCamera.startPreview();
//设置mTexture透明度
mTexture.setAlpha(1.0f);
//设置mTexture旋转角度
mTexture.setRotation(90.0f);
} catch (IOException ioe) {
// Something bad happened
} } /**
* SurfaceTexture的缓存大小改变了
*/@Overridepublic void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) { } /**
* SurfaceTexture销毁了
*/@Overridepublic boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
mCamera.stopPreview();
mCamera.release();
return true;
} /**
* SurfaceTexture更新了
*/@Overridepublic void onSurfaceTextureUpdated(SurfaceTexture surface) { }
}

xml文件中TextureView是这样定义的

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

注意:TextureView设置旋转90度才是我们的正常视角。

Android4.0新控件的更多相关文章

  1. 一个Activity掌握Android4.0新控件 (转)

    原文地址:http://blog.csdn.net/lavor_zl/article/details/51261380 谷歌在推出Android4.0的同时推出了一些新控件,Android4.0中最常 ...

  2. 一个Activity掌握Android5.0新控件 (转)

    原文地址:http://blog.csdn.net/lavor_zl/article/details/51279386 谷歌在推出Android5.0的同时推出了一些新控件,Android5.0中最常 ...

  3. Android 5.0新控件——FloatingActionButton(悬浮按钮)

    Android 5.0新控件--FloatingActionButton(悬浮按钮) FloatingActionButton是5.0以后的新控件,一个悬浮按钮,之所以叫做悬浮按钮,主要是因为自带阴影 ...

  4. 【Android】Anroid5.0+新控件---酷炫标题栏的简单学习

    Android5.0+推出的新控件感觉特别酷,最近想模仿大神做个看图App出来,所以先把这些新控件用熟悉了. 新控件的介绍.使用等等网上相应的文章已经特别多了,题主也没那能力去写篇详解出来,本篇随笔记 ...

  5. Android5.0新控件CardView的介绍和使用

       CardView也是5.0的新控件,这控件其实就是一个卡片啦,当然我们自己也完全可以定义这样一个卡片,从现在的微博等社App中可以看到各式各样的自定义卡片,所以这个控件意义不是很大.suppor ...

  6. Android5.0新控件

    谷歌在推出Android5.0的同时推出了一些新控件,Android5.0中最常用的新控件有下面5种.  1. CardView(卡片视图) CardView顾名思义是卡片视图,它继承FrameLay ...

  7. Android 5.0新控件——TextInputLayout

    Android 5.0(M)新控件--TextInputLayout 介绍之前,先直观的看一下效果 TextInputLayout其实是一个容器,他继承自LinearLayout,该容器是作用于Tex ...

  8. Android4.0 -- UI控件之 Menu 菜单的的使用(三)

    上一讲 [Android 开发]:UI控件之 Menu 菜单的的使用(二) 我们讲解了创建上下文菜单的第一种使用方式:Creating a floating context menu [创建悬浮的上下 ...

  9. Android4.0 -- UI控件之 Menu 菜单的的使用(二)

    上一讲我们讲解了android中在代码或者xml文件中定义菜单,这一讲我们继续来讲解一下定义菜单的其他方式:创建上下文的菜单.查看API文档 Menus :Creating Contextual Me ...

随机推荐

  1. 使用 requirejs 打包 jQuery 插件 datetimepicker 的问题记录

    网站之前用的时间选择 UI 实在太丑,而且功能单一,决定全站改用 https://github.com/xdan/datetimepicker/ 里面有好几个 js,奇怪的是,只有 /build 目录 ...

  2. 厉害了,龙果!开源中国颁发了证书:GVP-码云最有价值开源项目

    roncoo-pay (龙果支付系统) roncoo-pay是国内首款开源的互联网支付系统,其核心目标是汇聚所有主流支付渠道,打造一款轻量.便捷.易用,且集支付.资金对账.资金清结算于一体的支付系统, ...

  3. Android 屏幕刷新机制

    这次就来梳理一下 Android 的屏幕刷新机制,把我这段时间因为研究动画而梳理出来的一些关于屏幕刷新方面的知识点分享出来,能力有限,有错的地方还望指点一下.另外,内容有点多,毕竟要讲清楚不容易,所以 ...

  4. iptables网络安全服务详细使用

    iptables防火墙概念说明 开源的基于数据包过滤的网络安全策略控制工具. centos6.9  --- 默认防火墙工具软件iptables centos7    --- 默认防火墙工具软件fire ...

  5. HDU6237-A Simple Stone Game-找素因子(欧拉函数)-2017中国大学生程序设计竞赛-哈尔滨站-重现赛

    A Simple Stone Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Ot ...

  6. BZOJ2338: [HNOI2011]数矩形

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2338 中学数学老师告诉我们,一个矩形的两条对角线相等,所以只要把所有的边拿出来,记录下中点坐标 ...

  7. os系统

    任务延时函数OSTimeDly 功能:调用该函数的任务将自己延时一段时间并执行一次任务调度,一旦规定的延时时间完成或有其它的任务通过调用OSTimeDlyResume()取消了延时,调用OSTimeD ...

  8. ThinkPHP3.2基础知识(二)

    1.单入口模式的好处: 安全,灵活.可集中进行安全处理,访问统计等统一控制. 2.入口文件中为什么要判断PHP版本,TP要求PHP的版本是什么? 因为ThinkPHP3.2版本只能在PHP版本5.3. ...

  9. POJ 2253 Frogger(Dijkstra变形——最短路径最大权值)

    题目链接: http://poj.org/problem?id=2253 Description Freddy Frog is sitting on a stone in the middle of ...

  10. 修改织梦默认栏目页、文章页URL命名规则,简化目录结构

    版权声明:本文为博主原创文章,未经博主允许不得转载. 用织梦Dedecms建站,建议优化系统默认的URL结构,这样能简化网站目录深度,利于搜索引擎收录. 现在织梦的默认文章命名规则是 {typedir ...