在 Android 中各种布局的应用,以及菜单效果的实现

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



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

代码 <?xmlversion="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等,详见文档
-->
<LinearLayoutxmlns: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内的元素一层覆盖一层地显示
-->
<FrameLayoutandroid:layout_height="wrap_content"
android:layout_width
="fill_parent">
<TextViewandroid:layout_width="wrap_content"
android:layout_height
="wrap_content"android:text="FrameLayout">
</TextView>
<TextViewandroid:layout_width="wrap_content"
android:layout_height
="wrap_content"android:text="FrameLayout">
</TextView>
</FrameLayout>

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

<!--
TableLayout-表格式布局。
TableRow-表格内的行,行内每一个元素算作一列
collapseColumns-设置TableLayout内的TableRow中需要隐藏的列的列索引,多个用“,”隔开
stretchColumns-设置TableLayout内的TableRow中需要拉伸(该列会拉伸到所有可用空间)的列的列索引,多个用“,”隔开
shrinkColumns-设置TableLayout内的TableRow中需要收缩(为了使其他列不会被挤到屏幕外,此列会自动收缩)的列的列索引,多个用“,”隔开
-->
<TableLayoutandroid:id="@+id/TableLayout01"
android:layout_width
="fill_parent"android:layout_height="wrap_content"
android:collapseColumns
="1">
<TableRowandroid:id="@+id/TableRow01"android:layout_width="fill_parent"
android:layout_height
="wrap_content">
<TextViewandroid:layout_width="wrap_content"
android:layout_weight
="1"android:layout_height="wrap_content"
android:text
="行1列1"/>
<TextViewandroid:layout_width="wrap_content"
android:layout_weight
="1"android:layout_height="wrap_content"
android:text
="行1列2"/>
<TextViewandroid:layout_width="wrap_content"
android:layout_weight
="1"android:layout_height="wrap_content"
android:text
="行1列3"/>
</TableRow>
<TableRowandroid:id="@+id/TableRow01"android:layout_width="wrap_content"
android:layout_height
="wrap_content">
<TextViewandroid:layout_width="wrap_content"
android:layout_height
="wrap_content"android:text="行2列1"/>
</TableRow>
</TableLayout>

<!--
AbsoluteLayout-绝对定位布局。
layout_x-x坐标。以左上角为顶点
layout_y-y坐标。以左上角为顶点
-->
<AbsoluteLayoutandroid:layout_height="wrap_content"
android:layout_width
="fill_parent">
<TextViewandroid: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-当前元素与指定的元素右对齐
-->
<RelativeLayoutandroid:id="@+id/RelativeLayout01"
android:layout_width
="fill_parent"android:layout_height="fill_parent">
<TextViewandroid:layout_width="wrap_content"android:id="@+id/abc"
android:layout_height
="wrap_content"android:text="centerInParent=true"
android:layout_centerInParent
="true"/>
<TextViewandroid:layout_width="wrap_content"
android:layout_height
="wrap_content"android:text="marginLeft=20px"
android:layout_marginLeft
="20px"/>
<TextViewandroid: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

<?xmlversion="1.0"encoding="utf-8"?>
<resources>
<stringname="hello">HelloLayout</string>
<stringname="app_name">webabcd_layout</string>
</resources>


Main.java

代码 packagecom.webabcd.layout;

importandroid.app.Activity;
importandroid.os.Bundle;

publicclassMainextendsActivity{
/**Calledwhentheactivityisfirstcreated.*/
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}

更多相关文章

  1. Android的五种布局
  2. Android RecyclerView 多布局与混排
  3. android基础入门布局讲解(2)
  4. Android输入法挤乱布局问题
  5. 2019-08-20 Android 线性布局介绍-LinearLayout
  6. android 简易3d菜单(伪3d)
  7. Android相对属性布局总结

随机推荐

  1. 新年目标
  2. Android 中使用Aspectj进行切面编程
  3. android UDP广播、组播
  4. [GoogleMap/GPS] 申请Android API Key
  5. 使用Visual Studio 2015开发Android 程序
  6. android 5分钟集成环信及时聊天DEMO
  7. Android back键ANR,与onPause,onDestory “
  8. Android 累计未读通知的数量
  9. Mac Android(安卓)屏幕同步/共享投影工具
  10. 如何让Android中的 Base64工具类支持2.2