一個Activity掌握Android4.0新控制項

來源:互聯網
上載者:User

標籤:


原文地址:http://blog.csdn.net/lavor_zl/article/details/51261380

Google在推出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開啟時顯示的文本
怎麼監聽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度才是我們的正常視角。

6. 一個Activity掌握Android4.0新控制項

為了方便學習,本人將這5個新控制項放到一個Activity中進行使用。

  • 程式原始介面講解
  • 開啟Switch開關,點擊“開啟POPUPMENU”彈出PopupMenu
  • 下面分別是TextureView旋轉0度(即不旋轉),旋轉45度,旋轉90度三種情況講解


本程式原始碼下載一個Activity掌握Android4.0新控制項



來自為知筆記(Wiz)

一個Activity掌握Android4.0新控制項 (轉)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.