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

来源:互联网 发布:成都直通车淘宝 编辑:程序博客网 时间:2024/04/24 11:53

转自:http://www.cnblogs.com/webabcd/archive/2010/01/19/1651197.html
介绍
在 Android 中各种布局的应用,以及菜单效果的实现 
各种布局方式的应用,FrameLayout, LinearLayout, TableLayout, AbsoluteLayout, RelativeLayout 
为指定元素配置上下文菜单,为应用程序配置选项菜单,以及多级菜单的实现 


1、各种布局方式的演示
res/layout/main.xml

代码

  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <!--    
  3. layout_width - 宽。fill_parent: 宽度跟着父元素走;wrap_content: 宽度跟着本身的内容走;直接指定一个 px 值来设置宽   
  4. layout_height - 高。fill_parent: 高度跟着父元素走;wrap_content: 高度跟着本身的内容走;直接指定一个 px 值来设置高   
  5. -->   
  6.   
  7. <!--   
  8. LinearLayout - 线形布局。   
  9.     orientation - 容器内元素的排列方式。vertical: 子元素们垂直排列;horizontal: 子元素们水平排列   
  10.     gravity - 内容的排列形式。常用的有 top, bottom, left, right, center 等,详见文档   
  11. -->   
  12. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  13.     android:orientation="vertical" android:gravity="right"  
  14.     android:layout_width="fill_parent" android:layout_height="fill_parent">   
  15.   
  16.     <!--   
  17.     FrameLayout - 层叠式布局。以左上角为起点,将  FrameLayout 内的元素一层覆盖一层地显示   
  18.     -->   
  19.     <FrameLayout android:layout_height="wrap_content"  
  20.         android:layout_width="fill_parent">   
  21.         <TextView android:layout_width="wrap_content"  
  22.             android:layout_height="wrap_content" android:text="FrameLayout">   
  23.         </TextView>   
  24.         <TextView android:layout_width="wrap_content"  
  25.             android:layout_height="wrap_content" android:text="Frame Layout">   
  26.         </TextView>   
  27.     </FrameLayout>   
  28.   
  29.     <TextView android:layout_width="wrap_content"  
  30.         android:layout_height="wrap_content" android:text="@string/hello" />   
  31.   
  32.     <!--   
  33.     TableLayout - 表格式布局。   
  34.         TableRow - 表格内的行,行内每一个元素算作一列   
  35.         collapseColumns - 设置 TableLayout 内的 TableRow 中需要隐藏的列的列索引,多个用“,”隔开   
  36.         stretchColumns - 设置 TableLayout 内的 TableRow 中需要拉伸(该列会拉伸到所有可用空间)的列的列索引,多个用“,”隔开   
  37.         shrinkColumns - 设置 TableLayout 内的 TableRow 中需要收缩(为了使其他列不会被挤到屏幕外,此列会自动收缩)的列的列索引,多个用“,”隔开   
  38.     -->   
  39.     <TableLayout android:id="@+id/TableLayout01"  
  40.         android:layout_width="fill_parent" android:layout_height="wrap_content"  
  41.         android:collapseColumns="1">   
  42.         <TableRow android:id="@+id/TableRow01" android:layout_width="fill_parent"  
  43.             android:layout_height="wrap_content">   
  44.             <TextView android:layout_width="wrap_content"  
  45.                 android:layout_weight="1" android:layout_height="wrap_content"  
  46.                 android:text="行1列1" />   
  47.             <TextView android:layout_width="wrap_content"  
  48.                 android:layout_weight="1" android:layout_height="wrap_content"  
  49.                 android:text="行1列2" />   
  50.             <TextView android:layout_width="wrap_content"  
  51.                 android:layout_weight="1" android:layout_height="wrap_content"  
  52.                 android:text="行1列3" />   
  53.         </TableRow>   
  54.         <TableRow android:id="@+id/TableRow01" android:layout_width="wrap_content"  
  55.             android:layout_height="wrap_content">   
  56.             <TextView android:layout_width="wrap_content"  
  57.                 android:layout_height="wrap_content" android:text="行2列1" />   
  58.         </TableRow>   
  59.     </TableLayout>   
  60.   
  61.     <!--   
  62.     AbsoluteLayout - 绝对定位布局。   
  63.         layout_x - x 坐标。以左上角为顶点   
  64.         layout_y - y 坐标。以左上角为顶点   
  65.     -->   
  66.     <AbsoluteLayout android:layout_height="wrap_content"  
  67.         android:layout_width="fill_parent">   
  68.         <TextView android:layout_width="wrap_content"  
  69.             android:layout_height="wrap_content" android:text="AbsoluteLayout"  
  70.             android:layout_x="100px"    
  71.             android:layout_y="100px" />   
  72.     </AbsoluteLayout>   
  73.   
  74.     <!--   
  75.     RelativeLayout - 相对定位布局。   
  76.         layout_centerInParent - 将当前元素放置到其容器内的水平方向和垂直方向的中央位置(类似的属性有 :layout_centerHorizontal, layout_alignParentLeft 等)   
  77.         layout_marginLeft - 设置当前元素相对于其容器的左侧边缘的距离   
  78.         layout_below - 放置当前元素到指定的元素的下面   
  79.         layout_alignRight - 当前元素与指定的元素右对齐   
  80.     -->   
  81.     <RelativeLayout android:id="@+id/RelativeLayout01"  
  82.         android:layout_width="fill_parent" android:layout_height="fill_parent">   
  83.         <TextView android:layout_width="wrap_content" android:id="@+id/abc"  
  84.             android:layout_height="wrap_content" android:text="centerInParent=true"  
  85.             android:layout_centerInParent="true" />   
  86.         <TextView android:layout_width="wrap_content"  
  87.             android:layout_height="wrap_content" android:text="marginLeft=20px"  
  88.             android:layout_marginLeft="20px" />   
  89.         <TextView android:layout_width="wrap_content"  
  90.             android:layout_height="wrap_content" android:text="xxx"  
  91.             android:layout_below="@id/abc" android:layout_alignRight="@id/abc" />   
  92.     </RelativeLayout>   
  93.   
  94. </LinearLayout>   
  95.   
  96.   
  97. res/values/strings.xml    
  98. <?xml version="1.0" encoding="utf-8"?>   
  99. <resources>   
  100.     <string name="hello">Hello Layout</string>   
  101.     <string name="app_name">webabcd_layout</string>   
  102. </resources>   
  103.   
  104.   
  105. Main.java   
  106.   
  107. 代码    
  108. package com.webabcd.layout;   
  109.   
  110. import android.app.Activity;   
  111. import android.os.Bundle;   
  112.   
  113. public class Main extends Activity {   
  114.     /** Called when the activity is first created. */  
  115.     @Override  
  116.     public void onCreate(Bundle savedInstanceState) {   
  117.         super.onCreate(savedInstanceState);   
  118.         setContentView(R.layout.main);   
  119.     }   
  120. }   
  121.   
  122.   
  123. 2、上下文菜单,选项菜单,子菜单   
  124. res/layout/main.xml   
  125.   
  126. 代码    
  127. <?xml version="1.0" encoding="utf-8"?>   
  128. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  129.     android:orientation="vertical" android:layout_width="fill_parent"  
  130.     android:layout_height="fill_parent">   
  131.        
  132.     <TextView android:id="@+id/txt1" android:layout_width="fill_parent"  
  133.         android:layout_height="wrap_content" android:text="@string/hello_contextMenu" />   
  134.            
  135.     <TextView android:id="@+id/txt2" android:layout_width="fill_parent"  
  136.         android:layout_height="wrap_content" android:text="@string/hello_subMenu" />   
  137.            
  138. </LinearLayout>   
  139.   
  140.   
  141. res/values/strings.xml   
  142.   
  143. 代码    
  144. <?xml version="1.0" encoding="utf-8"?>   
  145. <resources>   
  146.     <string name="hello_contextMenu">Hello Context Menu</string>   
  147.     <string name="hello_subMenu">Hello Context Sub Menu</string>   
  148.     <string name="app_name">webabcd_menu</string>   
  149. </resources>   
  150.   
  151.   
  152. Main.java   
  153.   
  154. 代码    
  155. package com.webabcd.menu;   
  156.   
  157. import android.app.Activity;   
  158. import android.os.Bundle;   
  159. import android.view.ContextMenu;   
  160. import android.view.Menu;   
  161. import android.view.MenuItem;   
  162. import android.view.SubMenu;   
  163. import android.view.View;   
  164. import android.view.ContextMenu.ContextMenuInfo;   
  165. import android.widget.TextView;   
  166. import android.widget.Toast;   
  167.   
  168. // 演示两种菜单的实现方式:上下文菜单(通过在某元素上长按,来呼出菜单)和选项菜单(通过按手机上的菜单按钮,来呼出菜单)   
  169. public class Main extends Activity {   
  170.     /** Called when the activity is first created. */  
  171.     @Override  
  172.     public void onCreate(Bundle savedInstanceState) {   
  173.         super.onCreate(savedInstanceState);   
  174.         setContentView(R.layout.main);   
  175.   
  176.         // 为 R.id.txt1 注册一个上下文菜单(在此 TextView 上长按,则会呼出上下文菜单)   
  177.         // 具体呼出的菜单内容需要重写 onCreateContextMenu 来创建   
  178.         TextView txt1 = (TextView) this.findViewById(R.id.txt1);   
  179.         this.registerForContextMenu(txt1);   
  180.   
  181.         // 为 R.id.txt2 注册一个上下文菜单   
  182.         TextView txt2 = (TextView) this.findViewById(R.id.txt2);   
  183.         this.registerForContextMenu(txt2);   
  184.     }   
  185.   
  186.     // 重写 onCreateContextMenu 用以创建上下文菜单   
  187.     // 重写 onContextItemSelected 用以响应上下文菜单   
  188.     @Override  
  189.     public void onCreateContextMenu(ContextMenu menu, View v,   
  190.             ContextMenuInfo menuInfo) {   
  191.         super.onCreateContextMenu(menu, v, menuInfo);   
  192.   
  193.         // 创建 R.id.txt1 的上下文菜单   
  194.         if (v == (TextView) this.findViewById(R.id.txt1)) {   
  195.                
  196.             // ContextMenu.setIcon() - 设置菜单的图标   
  197.             // ContextMenu.setHeaderTitle() - 设置菜单的标题   
  198.             menu.setHeaderIcon(R.drawable.icon01);   
  199.             menu.setHeaderTitle("我是菜单");   
  200.                
  201.             // 用 ContextMenu.add() 来增加菜单项,返回值为 MenuItem   
  202.             // 第一个参数:组ID   
  203.             // 第二个参数:菜单项ID   
  204.             // 第三个参数:顺序号   
  205.             // 第四个参数:菜单项上显示的内容   
  206.             menu.add(100"菜单1");   
  207.                
  208.             // MenuItem - 新增菜单项后的返回类型,针对菜单项的其他设置在此对象上操作    
  209.             menu.add(111"菜单2").setCheckable(true);   
  210.                
  211.         }   
  212.         // 创建 R.id.txt2 的上下文菜单(多级上下文菜单)   
  213.         else if (v == (TextView) this.findViewById(R.id.txt2)) {   
  214.                
  215.             // ContextMenu.addSubMenu("菜单名称") - 用来添加子菜单。子菜单其实就是一个特殊的菜单   
  216.             SubMenu sub = menu.addSubMenu("父菜单1");   
  217.             sub.setIcon(R.drawable.icon01);   
  218.             sub.add(000"菜单1");   
  219.             sub.add(011"菜单2");   
  220.             sub.setGroupCheckable(1truetrue);   
  221.   
  222.             SubMenu sub2 = menu.addSubMenu("父菜单2");   
  223.             sub2.setIcon(R.drawable.icon01);   
  224.             sub2.add(100"菜单3");   
  225.             sub2.add(111"菜单4");   
  226.             sub2.setGroupCheckable(1truefalse);   
  227.                
  228.         }   
  229.     }   
  230.        
  231.        
  232.     // 重写 onCreateOptionsMenu 用以创建选项菜单   
  233.     @Override  
  234.     public boolean onCreateOptionsMenu(Menu menu) {   
  235.   
  236.         MenuItem menuItem = menu.add(000"菜单111111111111111111111");   
  237.            
  238.         // MenuItem.setIcon() - 设置菜单项的图标   
  239.         // MenuItem.setTitleCondensed() - 菜单的简标题,如果指定了简标题的话,菜单项上的标题将会以此简标题为准   
  240.         // MenuItem.setAlphabeticShortcut() - 设置选中此菜单项的快捷键   
  241.         // 注:菜单项超过 6 个的话,第 6 个菜单将会变为  More 菜单,多余的菜单会在单击 More 菜单之后显示出来   
  242.         menuItem.setIcon(R.drawable.icon01);   
  243.         menuItem.setTitleCondensed("菜单1");   
  244.         menuItem.setAlphabeticShortcut('a');   
  245.   
  246.         menu.add(011"菜单2").setIcon(R.drawable.icon02);   
  247.         menu.add(022"菜单3").setIcon(R.drawable.icon03);   
  248.         menu.add(033"菜单4");   
  249.         menu.add(044"菜单5");   
  250.         menu.add(055"菜单6");   
  251.         menu.add(066"菜单7").setIcon(R.drawable.icon04);   
  252.         menu.add(077"菜单8").setIcon(R.drawable.icon05);   
  253.   
  254.         return true;   
  255.     }   
  256.   
  257.     // 重写 onOptionsItemSelected 用以响应选项菜单   
  258.     @Override  
  259.     public boolean onOptionsItemSelected(MenuItem item) {   
  260.         super.onOptionsItemSelected(item);   
  261.            
  262.         Toast.makeText(Main.this"被单击的菜单项为:" + String.valueOf(item.getItemId()), Toast.LENGTH_SHORT).show();   
  263.   
  264.         return false;   
  265.     }   
  266. }  

原创粉丝点击