`

Android开发 - 布局

阅读更多

back >>

1. LinearLayout布局 (代码见Layout_01)

    * 四个属性

        - android:orientation  指定线性布局的朝向,vertical表示垂直布局,由上到下排版,horizontal表示由左到右排版

        - android:layout_weight 子元素对未被占用空间分配权重值

        - android:layout_gravity 本元素相对于父元素的重力方向

        - android:gravity 指定本元素的所有子元素的重力方向

    * 代码示例

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal/vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <!--
    	android:id  — 为控件指定相应的ID
    	android:text — 指定控件中显示的文字,需要注意的是,这里尽量使用strings.xml文件当中的字符串
    	android:grivity — 指定控件的基本位置,比如说居中,居右等位置
    	android:textSize — 指定控件当中字体的大小
    	android:background — 指定该控件所使用的背景色,RGB命名法 
    	android:width — 指定控件的宽度
    	android:height — 指定控件的高度
    	android:padding* — 指定控件的内边距,也就是说控件当中的内容
    	android:sigleLine — 如果设置为真的话,则将控件的内容在同一行当中进行显示(不折行)
    	android:layout_weight — 指示控件在当前activity中所占的高度比重,要考虑到当前内容大小,决定该值
     -->
	<TextView
		android:id="@+id/firstText"
		android:text="第一行很多字很多字很多字很多字"
		android:gravity="right"
		android:textSize="15pt"
		android:background="#aa0000"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:padding="30px"
		android:layout_weight="1"
        android:singleLine="true"/>
	<TextView
		android:id="@+id/secondText"
		android:text="第二行"
		android:gravity="center_vertical"
		android:textSize="15pt"
		android:background="#0000aa"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:layout_weight="1"/>
</LinearLayout>

    * 运行效果 android:orientation=horizontal

 

2. TableLayout布局 (代码见Layout_02)

    * 代码示例

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:stretchColumns="0">
    <!-- 
    	android:stretchColumns  拉伸指定列,以填满,列从0计数
     -->
    <TableRow android:layout_width="fill_parent">
        <TextView
            android:text="@string/row1_column1"
            android:background="#aa0000"
            android:padding="3dip" />
        <TextView
        	android:text="@string/row1_column1"
        	android:padding="3dip"
        	android:gravity="center_horizontal"
            android:background="#00aa00"
        	></TextView>
    </TableRow>
    <TableRow>
        <TextView
            android:text="@string/row2_column1"
             android:background="#0000aa"
            android:padding="3dip" />
        <TextView
            android:text="@string/row2_column2"
            android:gravity="right"
             android:background="#0044aa"
            android:padding="3dip" />
    </TableRow>
</TableLayout>

     * 运行效果

 

3. RelativeLayout布局 (代码见Layout_03)

    * 代码示例

<?xml version="1.0" encoding="utf-8"?>
	<!--
		android:layout_above 将该控件的底部至于给定ID的控件之上
		android:layout_below 将该控件的顶部至于给定ID的控件之下
		android:layout_toLeftOf 将该控件的右边缘和给定ID的控件的左边缘对齐
		android:layout_toRightOf 将该控件的左边缘和给定ID的控件的右边缘对齐

		android:layout_alignBaseline 该控件的baseline和给定ID的控件的baseline对齐
		android:layout_alignBottom 将该控件的底部边缘与给定ID控件的底部边缘
		android:layout_alignLeft 将该控件的左边缘与给定ID控件的左边缘对齐
		android:layout_alignRight 将该控件的右边缘与给定ID控件的右边缘对齐
		android:layout_alignTop 将给定控件的顶部边缘与给定ID控件的顶部对齐


		android:alignParentBottom 如果该值为true,则将该控件的底部和父控件的底部对齐
		android:layout_alignParentLeft 如果该值为true,则将该控件的左边与父控件的左边对齐
		android:layout_alignParentRight 如果该值为true,则将该控件的右边与父控件的右边对齐
		android:layout_alignParentTop 如果该值为true,则将空间的顶部与父控件的顶部对齐

		android:layout_centerHorizontal 如果值为真,该控件将被至于水平方向的中央
		android:layout_centerInParent 如果值为真,该控件将被至于父控件水平方向和垂直方向的中央
		android:layout_centerVertical 如果值为真,该控件将被至于垂直方向的中央
	-->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:padding="10px" >

    <TextView android:id="@+id/label" 
              android:layout_width="fill_parent" 
              android:layout_height="wrap_content" 
              android:text="Type here:" />

    <EditText android:id="@+id/entry" 
              android:layout_width="fill_parent" 
              android:layout_height="wrap_content" 
              android:background="@android:drawable/editbox_background"
              android:layout_below="@id/label" />
  
    <Button android:id="@+id/ok" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:layout_below="@id/entry"
            android:layout_alignParentRight="true"
            android:layout_marginLeft="10px"
            android:text="OK" />

    <Button android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@id/ok"
            android:layout_alignTop="@id/ok"
            android:text="Cancel" />
</RelativeLayout>

    * 效果

 

4. 混合布局 (代码见Layout_04)

    * 效果1----setContentView(R.layout.main);

    * 效果2----setContentView(R.layout.second);

 

  • 大小: 19.3 KB
  • 大小: 13 KB
  • 大小: 29.9 KB
  • 大小: 39.2 KB
  • 大小: 12.9 KB
分享到:
评论

相关推荐

    Android开发-RecyclerView-AndroidStudio(三)瀑布流和几种布局

    在"Android开发-RecyclerView-AndroidStudio(三)瀑布流和几种布局"中,我们将探讨如何设置和使用这两种关键组件。 1. **瀑布流布局**(StaggeredGridLayoutManager): 瀑布流布局在电商、图片分享等应用中常见,...

    Android代码-Android_Learning_Notes

    1、Android01--搭建Android开发环境 2、Android02--认识Activity 3、Android03--Context和Application 4、Android04--Android服务 5、Android05--Android服务通信 6、Android06--Android广播接收器 7、Android07--...

    Android项目-谁是歌手

    这个项目对于那些希望踏入Android开发领域的人来说,无疑是一个理想的起点,因为它不仅提供了实际的代码实现,还包含了详尽的注释,使得学习过程更加清晰易懂。 在Android开发中,一个完整的项目通常包括多个组件和...

    Android--开发--gallery重叠特效源码+注释.rar

    在Android应用开发中,"Gallery"组件是一种早期用于展示可滚动图像集合的视图。然而,在Android 3.0(API级别...通过研究源码和注释,开发者可以学习到自定义视图、动画、布局管理等方面的知识,提升Android开发技能。

    Android-SwipeFinishLayout仿今日头条评论详情页滑动退出效果

    在Android开发中,布局(Layout)的设计是至关重要的,它决定了应用程序的用户界面和交互体验。本项目"Android-SwipeFinishLayout"旨在实现一个类似于今日头条评论详情页的滑动退出效果,这种效果通常被称为滑动手势...

    android-studio-ide-193.6626763-windows.exe

    Kotlin已成为Android开发的首选语言,4.0版本提供了更完善的Kotlin编码助手,包括智能提示、代码转换以及对Kotlin协程的全面支持,助力开发者编写更安全、更简洁的代码。 在兼容性方面,Android Studio 4.0支持...

    Android开发--华清远见android培训课件教程

    ### Android开发——华清远见Android培训课程精要 #### 一、版权与版权声明 本教程由华清远见嵌入式培训中心提供,版权所有。未经官方明确许可,不得以任何形式复制或传播此文档的任何部分。文档中的信息如有更改,...

    Android开发--可视化UI设计DroidDraw.pdf

    ### Android开发——可视化UI设计DroidDraw #### DroidDraw:GUI可视化设计器 **DroidDraw**是一款专门针对Android开发者设计的图形用户界面(GUI)工具,它基于Java Swing技术实现,能够帮助开发者轻松构建出复杂...

    android 开发---QQ登陆页面的源码

    在Android开发中,模仿QQ登录页面是一个常见的学习实践,它能帮助开发者深入理解用户界面设计、网络请求处理以及第三方SDK的集成。这个压缩包文件"TestLogin"很可能包含了一个完整的Android Studio项目,用于演示...

    android-support-v4.jar android-support-v13.jar android-support-v7-gridlayout.jar

    在Android开发中,支持库(Support Library)是Google提供的一系列API,用于向后兼容旧版本的Android系统,同时也引入了一些新的特性和组件。这里提到的`android-support-v4.jar`、`android-support-v13.jar`和`...

    Android-Android流式布局FlowLayout实现关键字标签

    在Android应用开发中,布局(Layout)是构建用户界面的核心元素。流式布局(FlowLayout)是一种特殊的布局方式,它允许...这个项目是一个很好的学习素材,可以帮助开发者提升自定义布局的能力,丰富其Android开发经验。

    android-studio-ide-202.7486908-linux.tar.gz

    1. **JetBrains Kotlin支持**:Kotlin是Android开发的首选语言,Android Studio 4.2.2提供了强大的Kotlin支持,包括智能代码补全、语法高亮、代码重构等功能,使得Kotlin编程更加流畅。 2. **Android Gradle Plugin...

    Android开发--实现输入密码连接WIFI

    在Android开发中,连接Wi-Fi是一项常见的功能,尤其在移动应用中,用户可能需要自动或手动连接到特定的Wi-Fi网络。本项目提供了一个简单的实现,允许用户输入密码来连接Wi-Fi,并且在成功连接后能够保存配置,以便于...

    android-studio-2021.2.1.15-windows

    10. **Flutter和Dart支持**:虽然Android Studio主要针对原生Android开发,但随着Flutter的流行,它也提供了对Flutter框架和Dart语言的支持,方便跨平台开发。 总之,“android-studio-2021.2.1.15-windows”是...

    Android-Android的Flexbox布局

    在Android开发中,布局设计是构建用户界面的关键环节。Flexbox布局,源于Web开发中的CSS Flexbox布局模型,被引入到Android系统中,为开发者提供了一种更灵活、更强大的方式来排列和对齐组件。FlexboxLayout允许我们...

    深入浅出Android--Google手持设备应用程序设计

    总的来说,《深入浅出Android--Google手持设备应用程序设计》将带你走过Android开发的全过程,从基础到高级,从理论到实践,让你具备开发高质量Android应用的能力。书中的实例和讲解将使你更深入地理解Android系统,...

    Android应用开发-LinearLayout布局.pptx

    在Android应用开发中,界面布局的设计是至关重要的,因为它决定了应用程序的用户交互体验。`LinearLayout`是Android中最基础且常用的布局之一,它允许开发者将UI元素按照垂直或水平的顺序排列。本篇内容将深入探讨`...

Global site tag (gtag.js) - Google Analytics