`

系出名门Android(2) - 布局(Layout)和菜单(Menu)

阅读更多

介绍
在 Android 中各种布局的应用,以及菜单效果的实现 
各种布局方式的应用,FrameLayout, LinearLayout, TableLayout, AbsoluteLayout, RelativeLayout 
为指定元素配置上下文菜单,为应用程序配置选项菜单,以及多级菜单的实现 


1、各种布局方式的演示
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 来创建
        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 用以响应上下文菜单
    @Override
    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 用以创建选项菜单
    @Override
    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 用以响应选项菜单
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);
        
        Toast.makeText(Main.this, "被单击的菜单项为:" + String.valueOf(item.getItemId()), Toast.LENGTH_SHORT).show();

        return false;
    }
}

 

分享到:
评论

相关推荐

    系出名门Android系列文章整合

    综上所述,“系出名门Android系列文章整合”涵盖了Android开发的各个方面,无论你是初学者还是有一定经验的开发者,都能从中获取有价值的信息和指导。通过深入学习和实践,你可以成为一名出色的Android开发者。

    凯旋名门酒店裙楼商业提案-10页.pdf

    【文档标题】提到的是“凯旋名门酒店裙楼商业提案”,这...综上所述,这份提案详细阐述了凯旋名门酒店裙楼的商业潜力,分析了项目的优势与挑战,并提出了具有针对性的招商策略和业态布局建议,以实现商业价值的最大化。

    专题讲座2021-2022年凯旋名门酒店裙楼商业提案.doc

    【专题讲座2021-2022年凯旋名门酒店裙楼商业提案】是一份关于商业策划和招商的文档,主要讨论了维也纳酒店裙楼的商业开发策略。以下是根据文档内容提取的相关知识点: 1. **项目分析**: - 项目位于宜昌市伍家区桔...

    名门监理月报2期.doc

    【名门监理月报2期】是针对2014年9月1日至10月9日期间,名门项目基坑支护工程的监理工作进行的总结报告,由鹤山市建设工程监理有限公司的监理项目部编撰。该报告详细记录了工程的形象进度、质量控制、安全文明施工等...

    java笔试题选择题-AndroidBang:《Android江湖花名册》

    java笔试题选择题 记录知识点 ...主要提供一整套布局方案和布局间的组件复用的问题。 flexbox-layout Flexbox for Android,现在已经支持 RecycleView 名榜 卷一:兵器榜 MaterialChipsInput Implemen

    地产策划X年鼎福名门世家开盘仪式活动方案.pptx

    主题为“鼎福·名门世家全城激情开放”,旨在激发公众的热情和参与度。 3. **活动前提** - 客户基础:前期市场培育积累了足够的客户资源。 - 客户疑虑:通过活动消除客户对项目的疑虑,增强购买信心。 4. **...

    20210222-东方证券-融创服务-1516.HK-出身名门展露龙头潜质,成长性与确定性俱佳.pdf

    标题中包含了几个关键点:东方证券、融创服务、股票代码1516.HK、以及对融创服务的业务和投资潜力的评价——出身名门、展露龙头潜质、成长性与确定性俱佳。以下是对这些知识点的详细说明: 1. 东方证券股份有限公司...

    西门子S7-200系列PLC各种型号介绍.rar

    这款系列涵盖了多种型号,每种型号都有其独特的特性和功能,以满足不同规模和复杂性的控制系统需求。下面将详细介绍西门子S7-200系列PLC的主要型号及其特点。 1. **S7-200 SMART**:这是西门子S7-200系列的最新一代...

    名门监理月报1期.doc

    【名门监理月报1期】是鹤山市建设工程监理有限公司对名门项目基坑支护工程在2014年7月29日至8月31日期间的工作总结报告。该月报详细记录了工程的进度、质量控制、安全文明施工、监理工作情况以及存在的问题和建议。 ...

    名门国际策划提案.ppt

    1. **项目概述**:名门国际项目地处黄金位置,紧邻农业路与东明路交汇,建筑规模大,包括地上32层,地下2层,拥有酒店式公寓和小户型住宅。1-4层为商业用途,5层为转换层,6层及以上为居住空间。 2. **SWOT分析**: ...

    名门监理月报3期.doc

    【名门监理月报3期】是鹤山市建设工程监理有限公司对名门项目基坑支护工程在2014年10月10日至11月6日期间的工作总结。该月报主要包括以下几个方面的内容: 1. **本月工程形象进度**: - B、C区的喷锚工程完成了80%...

    名门3栋、4栋高层住宅及2#会所安全监理实施细则.doc

    【名门3栋、4栋高层住宅及2#会所安全监理实施细则】 这份文档是针对名门3栋、4栋高层住宅及2#会所工程的安全监理实施的详细规程,旨在确保建筑施工过程中的安全。工程由广东华虹房地产开发公司负责,位于鹤山市沙坪...

    融创服务-1516.HK-出身名门展露龙头潜质,成长性与确定性俱佳.rar

    7. **投资价值**:综合以上因素,给出对融创服务的投资评级和目标股价,为投资者提供参考。 总的来说,融创服务作为一家有实力的物业服务公司,其在资本市场上的表现备受关注。通过对上述知识点的深入探讨,投资者...

    名门联系人名单.docx

    【名门联系人名单.docx】文件显然是一份与建筑工程管理相关的文档,主要列出了在某一项目中的关键联系人及其职责。在这个特定的行业里,这些角色对于项目的顺利进行至关重要。以下是对各职位及其职责的详细说明: 1...

    汉口西北湖盛世名门项目定位报告.doc

    地块呈不规则形状,地势平坦,设计上考虑了主次出入口的设置,以减少外部交通干扰,并通过合理的建筑布局减少高压线影响,提升项目形象。 总结来说,汉口西北湖盛世名门项目具备优越的地理位置,丰富的周边配套,...

    【编号5】PPT快闪视频-名门APP产品发布PPT模板.pptx

    【编号5】PPT快闪视频-名门APP产品发布PPT模板.pptx

    名门国际策划提案.pptx

    位于农业路与东明路交汇处,项目总建筑面积约12万平方米,包括地上32层和地下2层。项目优势包括地处商务密集区,临近CBD,具有高规格的酒店标准设施和服务,以及多样化的户型设计。同时,通过对项目SWOT分析,强调了...

    名门3栋、4栋高层住宅及2#会所安全监理方案.doc

    名门3栋、4栋高层住宅及2#会所安全监理方案.doc

Global site tag (gtag.js) - Google Analytics