Android程序开发基础之——页面布局

移动开发 Android
Android程序开发基础。

1、gravity

gravity 这个英文单词是重心的意思,在这里就表示停靠位置的意思。

android:gravity是对元素本身说的,元素本身的文本显示在什么地方靠着换个属性设置,不过不设置默认是在左侧的。

android:layout_gravity是相对与它的父元素说的,说明元素显示在父元素的什么位置。

比如说button: android:layout_gravity 表示按钮在界面上的位置。 android:gravity表示button上的字在button上的位置。

可选值:top、bottom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizontal、center、fill、clip_vertical。

而且这些属性是可以多选的,用“|”分开,默认这个的值是:Gravity.LEFT

2、layout_weight

layout_weight 用于给一个线性布局中的诸多视图的重要度赋值。

所有的视图都有一个layout_weight值,默认为零,意思是需要显示多大的视图就占据多大的屏幕空间。若赋一个高于零的值,则将父视图中的可用空间分割,分割大小具体取决于每一个视图的layout_weight 值以及该值在当前屏幕布局的整体 layout_weight值和在其它视图屏幕布局的layout_weight值中所占的比率而定。

举个例子:比如说我们在 水平方向上有一个文本标签和两个文本编辑元素。

该文本标签并无指定layout_weight值,所以它将占据需要提供的最少空间。如果两个文本编辑元素每一个的layout_weight值都设置为1,则两者平分在父视图布局剩余的宽度(因为我们声明这两者的重要度相等)。如果两个文本编辑元素其中***个的layout_weight值设置为1,而第二个的设置为2,

则剩余空间的三分之二分给***个,三分之一分给第二个(数值越小,重要度越高)。

 

[[73020]]

 

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:background="@drawable/bg02"

android:orientation="horizontal" >

 

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="4"

android:orientation="vertical" >

 

android:id="@+id/txtSpace1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="" />

 

 

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="1"

android:orientation="vertical" >

 

android:id="@+id/txtSpace2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="" />

 

android:id="@+id/button1"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/mn_01" />

 

android:id="@+id/button2"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/mn_02" />

 

android:id="@+id/button3"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/mn_03" />

 

android:id="@+id/button4"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/mn_04" />

 

 

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="4"

android:orientation="vertical" >

 

android:id="@+id/txtSpace3"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="" />

 

【编辑推荐】

  1. Android 的中长期焦虑?
  2. 风河公司:Android是目前最好的选择
  3. droid碎片化严重:应用商购买400款手机测试

 

责任编辑:冰凝儿 来源: 博客园
相关推荐

2012-05-14 21:14:07

Android页面传值

2009-06-09 10:24:35

NetBeansStruts页面布局

2022-03-28 07:52:31

H5小游戏开发教程页面基础布局

2014-12-17 10:12:06

HybridAppFramewor页面布局

2018-06-08 15:28:31

Android开发程序

2021-08-12 15:01:09

鸿蒙HarmonyOS应用

2010-05-22 16:57:09

BlackBerry开

2010-09-03 10:58:45

DIVCSS

2016-11-04 10:47:27

微信小程序

2009-08-14 17:08:00

Android应用程序

2013-01-17 15:51:42

Android开发应用程序组件

2011-04-11 17:07:33

布局基础用户界面设计Android

2016-11-04 10:49:48

微信小程序

2012-12-31 10:36:58

Android开发RelativeLay布局

2010-09-14 08:53:06

DIVTable

2012-04-26 22:57:57

Android

2011-07-25 18:32:55

iPad 平板电脑 苹果

2017-09-08 16:45:14

移动

2009-07-14 15:42:38

MyEclipse开发

2010-09-09 16:36:36

DIV标签
点赞
收藏

51CTO技术栈公众号