<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    即興的靈感

    思維是一種藝術; 藝術需要靈感。

    博客好友

    最新評論

    Android界面布局(Layout)和菜單(Menu)

    Android中的各種布局方式的應用,主要包括FrameLayout,LinearLayout,TableLayout,AbsoluteLayout, RelativeLayout。本文主要介紹布局文件的使用以及Android中菜單的開發(fā)。

    1、下面是各種布局方式的演示
    布局文件在res/layout文件夾中,以下用res/layout/main.xml舉例
    <?xml version="1.0" encoding="utf-8"?>
    <!–
    layout_width – 寬。

    fill_parent: 寬度跟著父元素走;

    wrap_content: 寬度跟著本身的內容走;直接指定一個 px 值來設置寬
    layout_height – 高。

    fill_parent: 高度跟著父元素走;

    wrap_content: 高度跟著本身的內容走;直接指定一個 px 值來設置高
    –>

    <!–
    LinearLayout – 線形布局。
        orientation – 容器內元素的排列方式。vertical: 子元素們垂直排列;horizontal: 子元素們水平排列
        gravity – 內容的排列形式。常用的有 top, bottom, left, right, center 等,詳見文檔
    –>
    <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">

        <!–
        FrameLayout – 層疊式布局。以左上角為起點,將  FrameLayout 內的元素一層覆蓋一層地顯示
        –>
        <FrameLayout android:layout_height="wrap_content"
            android:layout_width="fill_parent">
            <TextView android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:text="FrameLayout">
            </TextView>
            <TextView android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:text="Frame Layout">
            </TextView>
        </FrameLayout>

        <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:text="@string/hello" />

        <!–

    TableLayout – 表格式布局。

    TableRow – 表格內的行,行內每一個元素算作一列

    collapseColumns – 設置 TableLayout 內的 TableRow 中需要隱藏的列的列索引,多個用“,”隔開

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

    shrinkColumns – 設置TableLayout內的TableRow中需要收縮(為了使其他列不會被擠到屏幕外,此列會自動收縮)的列的列索引,多個用“,”隔開
        –>
        <TableLayout android:id="@ id/TableLayout01"
            android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:collapseColumns="1">
            <TableRow android:id="@ id/TableRow01" android:layout_width="fill_parent"
                android:layout_height="wrap_content">
                <TextView android:layout_width="wrap_content"
                    android:layout_weight="1" android:layout_height="wrap_content"
                    android:text="行1列1" />
                <TextView android:layout_width="wrap_content"
                    android:layout_weight="1" android:layout_height="wrap_content"
                    android:text="行1列2" />
                <TextView android:layout_width="wrap_content"
                    android:layout_weight="1" android:layout_height="wrap_content"
                    android:text="行1列3" />
            </TableRow>
            <TableRow android:id="@ id/TableRow01" android:layout_width="wrap_content"
                android:layout_height="wrap_content">
                <TextView android:layout_width="wrap_content"
                    android:layout_height="wrap_content" android:text="行2列1" />
            </TableRow>
        </TableLayout>

        <!–
        AbsoluteLayout – 絕對定位布局。
            layout_x – x 坐標。以左上角為頂點
            layout_y – y 坐標。以左上角為頂點
        –>
        <AbsoluteLayout android:layout_height="wrap_content"
            android:layout_width="fill_parent">
            <TextView android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:text="AbsoluteLayout"
                android:layout_x="100px"
                android:layout_y="100px" />
        </AbsoluteLayout>

        <!–
        RelativeLayout – 相對定位布局。
            layout_centerInParent – 將當前元素放置到其容器內的水平方向和垂直方向的中央位置(類似的屬性有 :layout_centerHorizontal, layout_alignParentLeft 等)
            layout_marginLeft – 設置當前元素相對于其容器的左側邊緣的距離
            layout_below – 放置當前元素到指定的元素的下面
            layout_alignRight – 當前元素與指定的元素右對齊
        –>
        <RelativeLayout android:id="@ id/RelativeLayout01"
            android:layout_width="fill_parent" android:layout_height="fill_parent">
            <TextView android:layout_width="wrap_content" android:id="@ id/abc"
                android:layout_height="wrap_content" android:text="centerInParent=true"
                android:layout_centerInParent="true" />
            <TextView android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:text="marginLeft=20px"
                android:layout_marginLeft="20px" />
            <TextView android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:text="xxx"
                android:layout_below="@id/abc" android:layout_alignRight="@id/abc" />
        </RelativeLayout>

    </LinearLayout>

    資源文件之字符串定義文件res/values/strings.xml
    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string name="hello">Hello Layout</string>
        <string name="app_name">webabcd_layout</string>
    </resources>

    主程序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、上下文菜單,選項菜單,子菜單
    res/layout/main.xml
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TextView android:id="@ id/txt1" android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:text="@string/hello_contextMenu" />
        <TextView android:id="@ id/txt2" android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:text="@string/hello_subMenu" />
    </LinearLayout>

    res/values/strings.xml
    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string name="hello_contextMenu">Hello Context Menu</string>
        <string name="hello_subMenu">Hello Context Sub Menu</string>
        <string name="app_name">webabcd_menu</string>
    </resources>

    Main.java
    package com.webabcd.menu;

    import android.app.Activity;
    import android.os.Bundle;
    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;

    // 演示兩種菜單的實現方式:上下文菜單(通過在某元素上長按,來呼出菜單)和選項菜單(通過按手機上的菜單按鈕,來呼出菜單)
    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);

            // 為 R.id.txt1 注冊一個上下文菜單(在此 TextView 上長按,則會呼出上下文菜單)
            // 具體呼出的菜單內容需要重寫 onCreateContextMenu 來創(chuàng)建
            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 用以創(chuàng)建上下文菜單
        // 重寫 onContextItemSelected 用以響應上下文菜單
        @Override
        public void onCreateContextMenu(ContextMenu menu, View v,
                ContextMenuInfo menuInfo) {
            super.onCreateContextMenu(menu, v, menuInfo);

            // 創(chuàng)建 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);
            }
            // 創(chuàng)建 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 用以創(chuàng)建選項菜單
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {

            MenuItem menuItem = menu.add(0, 0, 0, "菜單111111111111111111111");
            // MenuItem.setIcon() – 設置菜單項的圖標
            // MenuItem.setTitleCondensed() – 菜單的簡標題,如果指定了簡標題的話,菜單項上的標題將會以此簡標題為準
            // MenuItem.setAlphabeticShortcut() – 設置選中此菜單項的快捷鍵
            // 注:菜單項超過 6 個的話,第 6 個菜單將會變?yōu)?nbsp; 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 用以響應選項菜單
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            super.onOptionsItemSelected(item);
            Toast.makeText(Main.this, "被單擊的菜單項為:"   String.valueOf(item.getItemId()), Toast.LENGTH_SHORT).show();

            return false;
        }
    }

    文章地址

    ?
    鳳凰涅槃/浴火重生/馬不停蹄/只爭朝夕
    ???? 隱姓埋名/低調華麗/簡單生活/完美人生

    posted on 2010-04-18 18:08 poetguo 閱讀(5266) 評論(0)  編輯  收藏 所屬分類: Android

    主站蜘蛛池模板: 亚洲午夜激情视频| 日韩免费视频播放| 久久亚洲国产视频| 青青操免费在线视频| 亚洲精品无码不卡在线播HE| xxxxx做受大片视频免费| 久久精品国产亚洲5555| 青青操视频在线免费观看| 亚洲日韩乱码中文无码蜜桃臀网站| A毛片毛片看免费| 亚洲国产成人片在线观看| 91免费福利精品国产| 亚洲剧情在线观看| 日本牲交大片免费观看| 特级毛片全部免费播放| 亚洲色欲久久久综合网| 一级毛片在线免费看| 亚洲啪啪免费视频| 国产精品免费小视频| 中文字幕无线码中文字幕免费| 亚洲av综合avav中文| 日韩视频在线精品视频免费观看| 亚洲中文字幕无码亚洲成A人片| 四虎影视精品永久免费| 中文永久免费观看网站| 亚洲成人高清在线观看| 国产免费av一区二区三区| a级片免费观看视频| 亚洲小说图片视频| 免费h成人黄漫画嘿咻破解版| 国内精品99亚洲免费高清| 亚洲一区二区三区播放在线 | 亚洲国产精品成人精品软件| 免费看大美女大黄大色| 成人网站免费大全日韩国产| 亚洲字幕在线观看| 亚洲精品国产V片在线观看| 99re这里有免费视频精品| 真人无码作爱免费视频| 亚洲黄色免费网站| 亚洲乱码中文字幕手机在线|