天天看點

系出名門Android(2) - 布局(Layout)和菜單(Menu)

<a href="http://webabcd.blog.51cto.com/1787395/341976" target="_blank">[索引頁]</a>

<a href="http://down.51cto.com/data/100088" target="_blank">[源碼下載下傳]</a>

系出名門Android(2) - 布局(Layout)和菜單(Menu)

介紹

在 Android 中各種布局的應用,以及菜單效果的實作 

各種布局方式的應用,FrameLayout, LinearLayout, TableLayout, AbsoluteLayout, RelativeLayout  

為指定元素配置上下文菜單,為應用程式配置選項菜單,以及多級菜單的實作 

1、各種布局方式的示範

res/layout/main.xml

&lt;?xml version="1.0" encoding="utf-8"?&gt; 

&lt;!--    

layout_width - 寬。fill_parent: 寬度跟着父元素走;wrap_content: 寬度跟着本身的内容走;直接指定一個 px 值來設定寬 

layout_height - 高。fill_parent: 高度跟着父元素走;wrap_content: 高度跟着本身的内容走;直接指定一個 px 值來設定高 

--&gt; 

&lt;!-- 

LinearLayout - 線形布局。 

        orientation - 容器内元素的排列方式。vertical: 子元素們垂直排列;horizontal: 子元素們水準排列 

        gravity - 内容的排列形式。常用的有 top, bottom, left, right, center 等,詳見文檔 

&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 

        android:orientation="vertical" android:gravity="right" 

        android:layout_width="fill_parent" android:layout_height="fill_parent"&gt; 

        &lt;!-- 

        FrameLayout - 層疊式布局。以左上角為起點,将    FrameLayout 内的元素一層覆寫一層地顯示 

        --&gt; 

        &lt;FrameLayout android:layout_height="wrap_content" 

                android:layout_width="fill_parent"&gt; 

                &lt;TextView android:layout_width="wrap_content" 

                        android:layout_height="wrap_content" android:text="FrameLayout"&gt; 

                &lt;/TextView&gt; 

                        android:layout_height="wrap_content" android:text="Frame Layout"&gt; 

        &lt;/FrameLayout&gt; 

        &lt;TextView android:layout_width="wrap_content" 

                android:layout_height="wrap_content" android:text="@string/hello" /&gt; 

        TableLayout - 表格式布局。 

                TableRow - 表格内的行,行内每一個元素算作一列 

                collapseColumns - 設定 TableLayout 内的 TableRow 中需要隐藏的列的列索引,多個用“,”隔開 

                stretchColumns - 設定 TableLayout 内的 TableRow 中需要拉伸(該列會拉伸到所有可用空間)的列的列索引,多個用“,”隔開 

                shrinkColumns - 設定 TableLayout 内的 TableRow 中需要收縮(為了使其他列不會被擠到螢幕外,此列會自動收縮)的列的列索引,多個用“,”隔開 

        &lt;TableLayout android:id="@+id/TableLayout01" 

                android:layout_width="fill_parent" android:layout_height="wrap_content" 

                android:collapseColumns="1"&gt; 

                &lt;TableRow android:id="@+id/TableRow01" android:layout_width="fill_parent" 

                        android:layout_height="wrap_content"&gt; 

                        &lt;TextView android:layout_width="wrap_content" 

                                android:layout_weight="1" android:layout_height="wrap_content" 

                                android:text="行1列1" /&gt; 

                                android:text="行1列2" /&gt; 

                                android:text="行1列3" /&gt; 

                &lt;/TableRow&gt; 

                &lt;TableRow android:id="@+id/TableRow01" android:layout_width="wrap_content" 

                                android:layout_height="wrap_content" android:text="行2列1" /&gt; 

        &lt;/TableLayout&gt; 

        AbsoluteLayout - 絕對定位布局。 

                layout_x - x 坐标。以左上角為頂點 

                layout_y - y 坐标。以左上角為頂點 

        &lt;AbsoluteLayout android:layout_height="wrap_content" 

                        android:layout_height="wrap_content" android:text="AbsoluteLayout" 

                        android:layout_x="100px"    

                        android:layout_y="100px" /&gt; 

        &lt;/AbsoluteLayout&gt; 

        RelativeLayout - 相對定位布局。 

                layout_centerInParent - 将目前元素放置到其容器内的水準方向和垂直方向的中央位置(類似的屬性有 :layout_centerHorizontal, layout_alignParentLeft 等) 

                layout_marginLeft - 設定目前元素相對于其容器的左側邊緣的距離 

                layout_below - 放置目前元素到指定的元素的下面 

                layout_alignRight - 目前元素與指定的元素右對齊 

        &lt;RelativeLayout android:id="@+id/RelativeLayout01" 

                android:layout_width="fill_parent" android:layout_height="fill_parent"&gt; 

                &lt;TextView android:layout_width="wrap_content" android:id="@+id/abc" 

                        android:layout_height="wrap_content" android:text="centerInParent=true" 

                        android:layout_centerInParent="true" /&gt; 

                        android:layout_height="wrap_content" android:text="marginLeft=20px" 

                        android:layout_marginLeft="20px" /&gt; 

                        android:layout_height="wrap_content" android:text="xxx" 

                        android:layout_below="@id/abc" android:layout_alignRight="@id/abc" /&gt; 

        &lt;/RelativeLayout&gt; 

&lt;/LinearLayout&gt;

res/values/strings.xml

&lt;resources&gt; 

        &lt;string name="hello"&gt;Hello Layout&lt;/string&gt; 

        &lt;string name="app_name"&gt;webabcd_layout&lt;/string&gt; 

&lt;/resources&gt;

Main.java

package com.webabcd.layout; 

import android.app.Activity; 

import android.os.Bundle; 

public class Main extends Activity { 

        /** Called when the activity is first created. */ 

        @Override 

        public void onCreate(Bundle savedInstanceState) { 

                super.onCreate(savedInstanceState); 

                setContentView(R.layout.main); 

        } 

}

2、上下文菜單,選項菜單,子菜單

        android:orientation="vertical" android:layout_width="fill_parent" 

        android:layout_height="fill_parent"&gt; 

        &lt;TextView android:id="@+id/txt1" android:layout_width="fill_parent" 

                android:layout_height="wrap_content" android:text="@string/hello_contextMenu" /&gt; 

        &lt;TextView android:id="@+id/txt2" android:layout_width="fill_parent" 

                android:layout_height="wrap_content" android:text="@string/hello_subMenu" /&gt; 

        &lt;string name="hello_contextMenu"&gt;Hello Context Menu&lt;/string&gt; 

        &lt;string name="hello_subMenu"&gt;Hello Context Sub Menu&lt;/string&gt; 

        &lt;string name="app_name"&gt;webabcd_menu&lt;/string&gt; 

package com.webabcd.menu; 

import android.view.ContextMenu; 

import android.view.Menu; 

import android.view.MenuItem; 

import android.view.SubMenu; 

import android.view.View; 

import android.view.ContextMenu.ContextMenuInfo; 

import android.widget.TextView; 

import android.widget.Toast; 

// 示範兩種菜單的實作方式:上下文菜單(通過在某元素上長按,來呼出菜單)和選項菜單(通過按手機上的菜單按鈕,來呼出菜單) 

                // 為 R.id.txt1 注冊一個上下文菜單(在此 TextView 上長按,則會呼出上下文菜單) 

                // 具體呼出的菜單内容需要重寫 onCreateContextMenu 來建立 

                TextView txt1 = (TextView) this.findViewById(R.id.txt1); 

                this.registerForContextMenu(txt1); 

                // 為 R.id.txt2 注冊一個上下文菜單 

                TextView txt2 = (TextView) this.findViewById(R.id.txt2); 

                this.registerForContextMenu(txt2); 

        // 重寫 onCreateContextMenu 用以建立上下文菜單 

        // 重寫 onContextItemSelected 用以響應上下文菜單 

        public void onCreateContextMenu(ContextMenu menu, View v, 

                        ContextMenuInfo menuInfo) { 

                super.onCreateContextMenu(menu, v, menuInfo); 

                // 建立 R.id.txt1 的上下文菜單 

                if (v == (TextView) this.findViewById(R.id.txt1)) { 

                        // ContextMenu.setIcon() - 設定菜單的圖示 

                        // ContextMenu.setHeaderTitle() - 設定菜單的标題 

                        menu.setHeaderIcon(R.drawable.icon01); 

                        menu.setHeaderTitle("我是菜單"); 

                        // 用 ContextMenu.add() 來增加菜單項,傳回值為 MenuItem 

                        // 第一個參數:組ID 

                        // 第二個參數:菜單項ID 

                        // 第三個參數:順序号 

                        // 第四個參數:菜單項上顯示的内容 

                        menu.add(1, 0, 0, "菜單1"); 

                        // MenuItem - 新增菜單項後的傳回類型,針對菜單項的其他設定在此對象上操作    

                        menu.add(1, 1, 1, "菜單2").setCheckable(true); 

                } 

                // 建立 R.id.txt2 的上下文菜單(多級上下文菜單) 

                else if (v == (TextView) this.findViewById(R.id.txt2)) { 

                        // ContextMenu.addSubMenu("菜單名稱") - 用來添加子菜單。子菜單其實就是一個特殊的菜單 

                        SubMenu sub = menu.addSubMenu("父菜單1"); 

                        sub.setIcon(R.drawable.icon01); 

                        sub.add(0, 0, 0, "菜單1"); 

                        sub.add(0, 1, 1, "菜單2"); 

                        sub.setGroupCheckable(1, true, true); 

                        SubMenu sub2 = menu.addSubMenu("父菜單2"); 

                        sub2.setIcon(R.drawable.icon01); 

                        sub2.add(1, 0, 0, "菜單3"); 

                        sub2.add(1, 1, 1, "菜單4"); 

                        sub2.setGroupCheckable(1, true, false); 

        // 重寫 onCreateOptionsMenu 用以建立選項菜單 

        public boolean onCreateOptionsMenu(Menu menu) { 

                MenuItem menuItem = menu.add(0, 0, 0, "菜單111111111111111111111"); 

                // MenuItem.setIcon() - 設定菜單項的圖示 

                // MenuItem.setTitleCondensed() - 菜單的簡标題,如果指定了簡标題的話,菜單項上的标題将會以此簡标題為準 

                // MenuItem.setAlphabeticShortcut() - 設定選中此菜單項的快捷鍵 

                // 注:菜單項超過 6 個的話,第 6 個菜單将會變為    More 菜單,多餘的菜單會在單擊 More 菜單之後顯示出來 

                menuItem.setIcon(R.drawable.icon01); 

                menuItem.setTitleCondensed("菜單1"); 

                menuItem.setAlphabeticShortcut('a'); 

                menu.add(0, 1, 1, "菜單2").setIcon(R.drawable.icon02); 

                menu.add(0, 2, 2, "菜單3").setIcon(R.drawable.icon03); 

                menu.add(0, 3, 3, "菜單4"); 

                menu.add(0, 4, 4, "菜單5"); 

                menu.add(0, 5, 5, "菜單6"); 

                menu.add(0, 6, 6, "菜單7").setIcon(R.drawable.icon04); 

                menu.add(0, 7, 7, "菜單8").setIcon(R.drawable.icon05); 

                return true; 

        // 重寫 onOptionsItemSelected 用以響應選項菜單 

        public boolean onOptionsItemSelected(MenuItem item) { 

                super.onOptionsItemSelected(item); 

                Toast.makeText(Main.this, "被單擊的菜單項為:" + String.valueOf(item.getItemId()), Toast.LENGTH_SHORT).show(); 

                return false; 

OK

     本文轉自webabcd 51CTO部落格,原文連結:http://blog.51cto.com/webabcd/342009,如需轉載請自行聯系原作者

繼續閱讀