现在的位置: 首页 > 综合 > 正文

Layout 和 Menu【转】

2013年12月10日 ⁄ 综合 ⁄ 共 8820字 ⁄ 字号 评论关闭

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

Java代码

  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. LinearLayout - 线形布局。   
  8.     orientation - 容器内元素的排列方式。vertical: 子元素们垂直排列;horizontal: 子元素们水平排列   
  9.     gravity - 内容的排列形式。常用的有 top, bottom, left, right, center 等,详见文档   
  10. -->   
  11. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  12.     android:orientation="vertical" android:gravity="right"
  13.     android:layout_width="fill_parent" android:layout_height="fill_parent">   
  14.     <!--   
  15.     FrameLayout - 层叠式布局。以左上角为起点,将  FrameLayout 内的元素一层覆盖一层地显示   
  16.     -->   
  17.     <FrameLayout android:layout_height="wrap_content"
  18.         android:layout_width="fill_parent">   
  19.         <TextView android:layout_width="wrap_content"
  20.             android:layout_height="wrap_content" android:text="FrameLayout">   
  21.         </TextView>   
  22.         <TextView android:layout_width="wrap_content"
  23.             android:layout_height="wrap_content" android:text="Frame Layout">   
  24.         </TextView>   
  25.     </FrameLayout>   
  26.     <TextView android:layout_width="wrap_content"
  27.         android:layout_height="wrap_content" android:text="@string/hello" />   
  28.     <!--   
  29.     TableLayout - 表格式布局。   
  30.         TableRow - 表格内的行,行内每一个元素算作一列   
  31.         collapseColumns - 设置 TableLayout 内的 TableRow 中需要隐藏的列的列索引,多个用“,”隔开   
  32.         stretchColumns - 设置 TableLayout 内的 TableRow 中需要拉伸(该列会拉伸到所有可用空间)的列的列索引,多个用“,”隔开   
  33.         shrinkColumns - 设置 TableLayout 内的 TableRow 中需要收缩(为了使其他列不会被挤到屏幕外,此列会自动收缩)的列的列索引,多个用“,”隔开   
  34.     -->   
  35.     <TableLayout android:id="@+id/TableLayout01"
  36.         android:layout_width="fill_parent" android:layout_height="wrap_content"
  37.         android:collapseColumns="1">   
  38.         <TableRow android:id="@+id/TableRow01" android:layout_width="fill_parent"
  39.             android:layout_height="wrap_content">   
  40.             <TextView android:layout_width="wrap_content"
  41.                 android:layout_weight="1" android:layout_height="wrap_content"
  42.                 android:text="行1列1" />   
  43.             <TextView android:layout_width="wrap_content"
  44.                 android:layout_weight="1" android:layout_height="wrap_content"
  45.                 android:text="行1列2" />   
  46.             <TextView android:layout_width="wrap_content"
  47.                 android:layout_weight="1" android:layout_height="wrap_content"
  48.                 android:text="行1列3" />   
  49.         </TableRow>   
  50.         <TableRow android:id="@+id/TableRow01" android:layout_width="wrap_content"
  51.             android:layout_height="wrap_content">   
  52.             <TextView android:layout_width="wrap_content"
  53.                 android:layout_height="wrap_content" android:text="行2列1" />   
  54.         </TableRow>   
  55.     </TableLayout>   
  56.     <!--   
  57. AbsoluteLayout - 绝对定位布局。   
  58.         layout_x - x 坐标。以左上角为顶点   
  59.         layout_y - y 坐标。以左上角为顶点   
  60.     -->   
  61.     <AbsoluteLayout android:layout_height="wrap_content"
  62.         android:layout_width="fill_parent">   
  63.         <TextView android:layout_width="wrap_content"
  64.             android:layout_height="wrap_content" android:text="AbsoluteLayout"
  65.             android:layout_x="100px"
  66.             android:layout_y="100px" />   
  67.     </AbsoluteLayout>   
  68.     <!--   
  69. RelativeLayout - 相对定位布局。   
  70.         layout_centerInParent - 将当前元素放置到其容器内的水平方向和垂直方向的中央位置(类似的属性有 :layout_centerHorizontal, layout_alignParentLeft 等)   
  71.         layout_marginLeft - 设置当前元素相对于其容器的左侧边缘的距离   
  72.         layout_below - 放置当前元素到指定的元素的下面   
  73.         layout_alignRight - 当前元素与指定的元素右对齐   
  74.     -->   
  75.     <RelativeLayout android:id="@+id/RelativeLayout01"
  76.         android:layout_width="fill_parent" android:layout_height="fill_parent">   
  77.         <TextView android:layout_width="wrap_content" android:id="@+id/abc"
  78.             android:layout_height="wrap_content" android:text="centerInParent=true"
  79.             android:layout_centerInParent="true" />   
  80.         <TextView android:layout_width="wrap_content"
  81.             android:layout_height="wrap_content" android:text="marginLeft=20px"
  82.             android:layout_marginLeft="20px" />   
  83.         <TextView android:layout_width="wrap_content"
  84.             android:layout_height="wrap_content" android:text="xxx"
  85.             android:layout_below="@id/abc" android:layout_alignRight="@id/abc" />   
  86.     </RelativeLayout>   
  87. </LinearLayout>   
  88. res/values/strings.xml    
  89. <?xml version="1.0" encoding="utf-8"?>   
  90. <resources>   
  91.     <string name="hello">Hello Layout</string>   
  92.     <string name="app_name">webabcd_layout</string>   
  93. </resources>   
  94. Main.java   
  95. 代码    
  96. package com.webabcd.layout;   
  97. import android.app.Activity;   
  98. import android.os.Bundle;   
  99. public class Main extends Activity {   
  100. /** Called when the activity is first created. */
  101. @Override
  102. public void onCreate(Bundle savedInstanceState) {   
  103. super.onCreate(savedInstanceState);   
  104.         setContentView(R.layout.main);   
  105.     }   
  106. }   

 

2、上下文菜单,选项菜单,子菜单  

  1. res/layout/main.xml   
  2. 代码    
  3. <?xml version="1.0" encoding="utf-8"?>   
  4. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  5.     android:orientation="vertical" android:layout_width="fill_parent"
  6.     android:layout_height="fill_parent">   
  7.     <TextView android:id="@+id/txt1" android:layout_width="fill_parent"
  8.         android:layout_height="wrap_content" android:text="@string/hello_contextMenu" />   
  9.     <TextView android:id="@+id/txt2" android:layout_width="fill_parent"
  10.         android:layout_height="wrap_content" android:text="@string/hello_subMenu" />   
  11. </LinearLayout>   
  12. res/values/strings.xml   
  13. 代码    
  14. <?xml version="1.0" encoding="utf-8"?>   
  15. <resources>   
  16.     <string name="hello_contextMenu">Hello Context Menu</string>   
  17.     <string name="hello_subMenu">Hello Context Sub Menu</string>   
  18.     <string name="app_name">webabcd_menu</string>   
  19. </resources>   
  20. Main.java   
  21. 代码    
  22. package com.webabcd.menu;   
  23. import android.app.Activity;   
  24. import android.os.Bundle;   
  25. import android.view.ContextMenu;   
  26. import android.view.Menu;   
  27. import android.view.MenuItem;   
  28. import android.view.SubMenu;   
  29. import android.view.View;   
  30. import android.view.ContextMenu.ContextMenuInfo;   
  31. import android.widget.TextView;   
  32. import android.widget.Toast;   
  33. // 演示两种菜单的实现方式:上下文菜单(通过在某元素上长按,来呼出菜单)和选项菜单(通过按手机上的菜单按钮,来呼出菜单)
  34. public class Main extends Activity {   
  35. /** Called when the activity is first created. */
  36. @Override
  37. public void onCreate(Bundle savedInstanceState) {   
  38. super.onCreate(savedInstanceState);   
  39.         setContentView(R.layout.main);   
  40. // 为 R.id.txt1 注册一个上下文菜单(在此 TextView 上长按,则会呼出上下文菜单)
  41. // 具体呼出的菜单内容需要重写 onCreateContextMenu 来创建
  42.         TextView txt1 = (TextView) this.findViewById(R.id.txt1);   
  43. this.registerForContextMenu(txt1);   
  44. // 为 R.id.txt2 注册一个上下文菜单
  45.         TextView txt2 = (TextView) this.findViewById(R.id.txt2);   
  46. this.registerForContextMenu(txt2);   
  47.     }   
  48. // 重写 onCreateContextMenu 用以创建上下文菜单
  49. // 重写 onContextItemSelected 用以响应上下文菜单
  50. @Override
  51. public void onCreateContextMenu(ContextMenu menu, View v,   
  52.             ContextMenuInfo menuInfo) {   
  53. super.onCreateContextMenu(menu, v, menuInfo);   
  54. // 创建 R.id.txt1 的上下文菜单
  55. if (v == (TextView) this.findViewById(R.id.txt1)) {   
  56. // ContextMenu.setIcon() - 设置菜单的图标
  57. // ContextMenu.setHeaderTitle() - 设置菜单的标题
  58.             menu.setHeaderIcon(R.drawable.icon01);   
  59.             menu.setHeaderTitle("我是菜单");   
  60. // 用 ContextMenu.add() 来增加菜单项,返回值为 MenuItem
  61. // 第一个参数:组ID
  62. // 第二个参数:菜单项ID
  63. // 第三个参数:顺序号
  64. // 第四个参数:菜单项上显示的内容
  65.             menu.add(1, 0, 0, "菜单1");   
  66. // MenuItem - 新增菜单项后的返回类型,针对菜单项的其他设置在此对象上操作 
  67.             menu.add(1, 1, 1, "菜单2").setCheckable(true);   
  68.         }   
  69. // 创建 R.id.txt2 的上下文菜单(多级上下文菜单)
  70. else if (v == (TextView) this.findViewById(R.id.txt2)) {   
  71. // ContextMenu.addSubMenu("菜单名称") - 用来添加子菜单。子菜单其实就是一个特殊的菜单
  72.             SubMenu sub = menu.addSubMenu("父菜单1");   
  73.             sub.setIcon(R.drawable.icon01);   
  74.             sub.add(0, 0, 0, "菜单1");   
  75.             sub.add(0, 1, 1, "菜单2");   
  76.             sub.setGroupCheckable(1, true, true);   
  77.             SubMenu sub2 = menu.addSubMenu("父菜单2");   
  78.             sub2.setIcon(R.drawable.icon01);   
  79.             sub2.add(1, 0, 0, "菜单3");   
  80.             sub2.add(1, 1, 1, "菜单4");   
  81.             sub2.setGroupCheckable(1, true, false);   
  82.         }   
  83.     }   
  84. // 重写 onCreateOptionsMenu 用以创建选项菜单
  85. @Override
  86. public boolean onCreateOptionsMenu(Menu menu) {   
  87.         MenuItem menuItem = menu.add(0, 0, 0, "菜单111111111111111111111");   
  88. // MenuItem.setIcon() - 设置菜单项的图标
  89. // MenuItem.setTitleCondensed() - 菜单的简标题,如果指定了简标题的话,菜单项上的标题将会以此简标题为准
  90. // MenuItem.setAlphabeticShortcut() - 设置选中此菜单项的快捷键
  91. // 注:菜单项超过 6 个的话,第 6 个菜单将会变为  More 菜单,多余的菜单会在单击 More 菜单之后显示出来
  92.         menuItem.setIcon(R.drawable.icon01);   
  93.         menuItem.setTitleCondensed("菜单1");   
  94.         menuItem.setAlphabeticShortcut('a');   
  95.         menu.add(0, 1, 1, "菜单2").setIcon(R.drawable.icon02);   
  96.         menu.add(0, 2, 2, "菜单3").setIcon(R.drawable.icon03);   
  97.         menu.add(0, 3, 3, "菜单4");   
  98.         menu.add(0, 4, 4, "菜单5");   
  99.         menu.add(0, 5, 5, "菜单6");   
  100.         menu.add(0, 6, 6, "菜单7").setIcon(R.drawable.icon04);   
  101.         menu.add(0, 7, 7, "菜单8").setIcon(R.drawable.icon05);   
  102. return true;   
  103.     }   
  104. // 重写 onOptionsItemSelected 用以响应选项菜单
  105. @Override
  106. public boolean onOptionsItemSelected(MenuItem item) {   
  107. super.onOptionsItemSelected(item);   
  108.         Toast.makeText(Main.this, "被单击的菜单项为:" + String.valueOf(item.getItemId()), Toast.LENGTH_SHORT).show();   
  109. return false;   
  110.     }   

抱歉!评论已关闭.