`
oraclestudy
  • 浏览: 480612 次
文章分类
社区版块
存档分类

AndroidGUI17:Layout常用技巧

 
阅读更多

<!-- [if !mso]> <style> v/:* {behavior:url(#default#VML);} o/:* {behavior:url(#default#VML);} w/:* {behavior:url(#default#VML);} .shape {behavior:url(#default#VML);} </style> <![endif]--><!-- [if gte mso 9]><xml> <o:OfficeDocumentSettings> <o:RelyOnVML/> <o:AllowPNG/> </o:OfficeDocumentSettings> </xml><![endif]--><!-- [if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:TrackMoves>false</w:TrackMoves> <w:TrackFormatting/> <w:PunctuationKerning/> <w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing> <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery> <w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery> <w:ValidateAgainstSchemas/> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:DoNotPromoteQF/> <w:LidThemeOther>EN-US</w:LidThemeOther> <w:LidThemeAsian>ZH-CN</w:LidThemeAsian> <w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript> <w:Compatibility> <w:SpaceForUL/> <w:BalanceSingleByteDoubleByteWidth/> <w:DoNotLeaveBackslashAlone/> <w:ULTrailSpace/> <w:DoNotExpandShiftReturn/> <w:AdjustLineHeightInTable/> <w:BreakWrappedTables/> <w:SnapToGridInCell/> <w:WrapTextWithPunct/> <w:UseAsianBreakRules/> <w:DontGrowAutofit/> <w:SplitPgBreakAndParaMark/> <w:DontVertAlignCellWithSp/> <w:DontBreakConstrainedForcedTables/> <w:DontVertAlignInTxbx/> <w:Word11KerningPairs/> <w:CachedColBalance/> <w:UseFELayout/> </w:Compatibility> <m:mathPr> <m:mathFont m:val="Cambria Math"/> <m:brkBin m:val="before"/> <m:brkBinSub m:val="--"/> <m:smallFrac m:val="off"/> <m:dispDef/> <m:lMargin m:val="0"/> <m:rMargin m:val="0"/> <m:defJc m:val="centerGroup"/> <m:wrapIndent m:val="1440"/> <m:intLim m:val="subSup"/> <m:naryLim m:val="undOvr"/> </m:mathPr></w:WordDocument> </xml><![endif]--><!-- [if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" DefUnhideWhenUsed="true" DefSemiHidden="true" DefQFormat="false" DefPriority="99" LatentStyleCount="267"> <w:LsdException Locked="false" Priority="0" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Normal"/> <w:LsdException Locked="false" Priority="9" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="heading 1"/> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 2"/> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 3"/> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 4"/> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 5"/> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 6"/> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 7"/> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 8"/> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 9"/> <w:LsdException Locked="false" Priority="39" Name="toc 1"/> <w:LsdException Locked="false" Priority="39" Name="toc 2"/> <w:LsdException Locked="false" Priority="39" Name="toc 3"/> <w:LsdException Locked="false" Priority="39" Name="toc 4"/> <w:LsdException Locked="false" Priority="39" Name="toc 5"/> <w:LsdException Locked="false" Priority="39" Name="toc 6"/> <w:LsdException Locked="false" Priority="39" Name="toc 7"/> <w:LsdException Locked="false" Priority="39" Name="toc 8"/> <w:LsdException Locked="false" Priority="39" Name="toc 9"/> <w:LsdException Locked="false" Priority="35" QFormat="true" Name="caption"/> <w:LsdException Locked="false" Priority="10" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Title"/> <w:LsdException Locked="false" Priority="1" Name="Default Paragraph Font"/> <w:LsdException Locked="false" Priority="11" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Subtitle"/> <w:LsdException Locked="false" Priority="22" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Strong"/> <w:LsdException Locked="false" Priority="20" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Emphasis"/> <w:LsdException Locked="false" Priority="59" SemiHidden="false" UnhideWhenUsed="false" Name="Table Grid"/> <w:LsdException Locked="false" UnhideWhenUsed="false" Name="Placeholder Text"/> <w:LsdException Locked="false" Priority="1" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="No Spacing"/> <w:LsdException Locked="false" Priority="60" SemiHidden="false" UnhideWhenUsed="false" Name="Light Shading"/> <w:LsdException Locked="false" Priority="61" SemiHidden="false" UnhideWhenUsed="false" Name="Light List"/> <w:LsdException Locked="false" Priority="62" SemiHidden="false" UnhideWhenUsed="false" Name="Light Grid"/> <w:LsdException Locked="false" Priority="63" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 1"/> <w:LsdException Locked="false" Priority="64" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 2"/> <w:LsdException Locked="false" Priority="65" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 1"/> <w:LsdException Locked="false" Priority="66" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 2"/> <w:LsdException Locked="false" Priority="67" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 1"/> <w:LsdException Locked="false" Priority="68" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 2"/> <w:LsdException Locked="false" Priority="69" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 3"/> <w:LsdException Locked="false" Priority="70" SemiHidden="false" UnhideWhenUsed="false" Name="Dark List"/> <w:LsdException Locked="false" Priority="71" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Shading"/> <w:LsdException Locked="false" Priority="72" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful List"/> <w:LsdException Locked="false" Priority="73" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Grid"/> <w:LsdException Locked="false" Priority="60" SemiHidden="false" UnhideWhenUsed="false" Name="Light Shading Accent 1"/> <w:LsdException Locked="false" Priority="61" SemiHidden="false" UnhideWhenUsed="false" Name="Light List Accent 1"/> <w:LsdException Locked="false" Priority="62" SemiHidden="false" UnhideWhenUsed="false" Name="Light Grid Accent 1"/> <w:LsdException Locked="false" Priority="63" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 1 Accent 1"/> <w:LsdException Locked="false" Priority="64" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 2 Accent 1"/> <w:LsdException Locked="false" Priority="65" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 1 Accent 1"/> <w:LsdException Locked="false" UnhideWhenUsed="false" Name="Revision"/> <w:LsdException Locked="false" Priority="34" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="List Paragraph"/> <w:LsdException Locked="false" Priority="29" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Quote"/> <w:LsdException Locked="false" Priority="30" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Intense Quote"/> <w:LsdException Locked="false" Priority="66" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 2 Accent 1"/> <w:LsdException Locked="false" Priority="67" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 1 Accent 1"/> <w:LsdException Locked="false" Priority="68" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 2 Accent 1"/> <w:LsdException Locked="false" Priority="69" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 3 Accent 1"/> <w:LsdException Locked="false" Priority="70" SemiHidden="false" UnhideWhenUsed="false" Name="Dark List Accent 1"/> <w:LsdException Locked="false" Priority="71" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Shading Accent 1"/> <w:LsdException Locked="false" Priority="72" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful List Accent 1"/> <w:LsdException Locked="false" Priority="73" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Grid Accent 1"/> <w:LsdException Locked="false" Priority="60" SemiHidden="false" UnhideWhenUsed="false" Name="Light Shading Accent 2"/> <w:LsdException Locked="false" Priority="61" SemiHidden="false" UnhideWhenUsed="false" Name="Light List Accent 2"/> <w:LsdException Locked="false" Priority="62" SemiHidden="false" UnhideWhenUsed="false" Name="Light Grid Accent 2"/> <w:LsdException Locked="false" Priority="63" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 1 Accent 2"/> <w:LsdException Locked="false" Priority="64" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 2 Accent 2"/> <w:LsdException Locked="false" Priority="65" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 1 Accent 2"/> <w:LsdException Locked="false" Priority="66" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 2 Accent 2"/> <w:LsdException Locked="false" Priority="67" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 1 Accent 2"/> <w:LsdException Locked="false" Priority="68" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 2 Accent 2"/> <w:LsdException Locked="false" Priority="69" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 3 Accent 2"/> <w:LsdException Locked="false" Priority="70" SemiHidden="false" UnhideWhenUsed="false" Name="Dark List Accent 2"/> <w:LsdException Locked="false" Priority="71" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Shading Accent 2"/> <w:LsdException Locked="false" Priority="72" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful List Accent 2"/> <w:LsdException Locked="false" Priority="73" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Grid Accent 2"/> <w:LsdException Locked="false" Priority="60" SemiHidden="false" UnhideWhenUsed="false" Name="Light Shading Accent 3"/> <w:LsdException Locked="false" Priority="61" SemiHidden="false" UnhideWhenUsed="false" Name="Light List Accent 3"/> <w:LsdException Locked="false" Priority="62" SemiHidden="false" UnhideWhenUsed="false" Name="Light Grid Accent 3"/> <w:LsdException Locked="false" Priority="63" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 1 Accent 3"/> <w:LsdException Locked="false" Priority="64" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 2 Accent 3"/> <w:LsdException Locked="false" Priority="65" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 1 Accent 3"/> <w:LsdException Locked="false" Priority="66" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 2 Accent 3"/> <w:LsdException Locked="false" Priority="67" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 1 Accent 3"/> <w:LsdException Locked="false" Priority="68" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 2 Accent 3"/> <w:LsdException Locked="false" Priority="69" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 3 Accent 3"/> <w:LsdException Locked="false" Priority="70" SemiHidden="false" UnhideWhenUsed="false" Name="Dark List Accent 3"/> <w:LsdException Locked="false" Priority="71" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Shading Accent 3"/> <w:LsdException Locked="false" Priority="72" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful List Accent 3"/> <w:LsdException Locked="false" Priority="73" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Grid Accent 3"/> <w:LsdException Locked="false" Priority="60" SemiHidden="false" UnhideWhenUsed="false" Name="Light Shading Accent 4"/> <w:LsdException Locked="false" Priority="61" SemiHidden="false" UnhideWhenUsed="false" Name="Light List Accent 4"/> <w:LsdException Locked="false" Priority="62" SemiHidden="false" UnhideWhenUsed="false" Name="Light Grid Accent 4"/> <w:LsdException Locked="false" Priority="63" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 1 Accent 4"/> <w:LsdException Locked="false" Priority="64" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 2 Accent 4"/> <w:LsdException Locked="false" Priority="65" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 1 Accent 4"/> <w:LsdException Locked="false" Priority="66" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 2 Accent 4"/> <w:LsdException Locked="false" Priority="67" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 1 Accent 4"/> <w:LsdException Locked="false" Priority="68" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 2 Accent 4"/> <w:LsdException Locked="false" Priority="69" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 3 Accent 4"/> <w:LsdException Locked="false" Priority="70" SemiHidden="false" UnhideWhenUsed="false" Name="Dark List Accent 4"/> <w:LsdException Locked="false" Priority="71" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Shading Accent 4"/> <w:LsdException Locked="false" Priority="72" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful List Accent 4"/> <w:LsdException Locked="false" Priority="73" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Grid Accent 4"/> <w:LsdException Locked="false" Priority="60" SemiHidden="false" UnhideWhenUsed="false" Name="Light Shading Accent 5"/> <w:LsdException Locked="false" Priority="61" SemiHidden="false" UnhideWhenUsed="false" Name="Light List Accent 5"/> <w:LsdException Locked="false" Priority="62" SemiHidden="false" UnhideWhenUsed="false" Name="Light Grid Accent 5"/> <w:LsdException Locked="false" Priority="63" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 1 Accent 5"/> <w:LsdException Locked="false" Priority="64" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 2 Accent 5"/> <w:LsdException Locked="false" Priority="65" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 1 Accent 5"/> <w:LsdException Locked="false" Priority="66" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 2 Accent 5"/> <w:LsdException Locked="false" Priority="67" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 1 Accent 5"/> <w:LsdException Locked="false" Priority="68" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 2 Accent 5"/> <w:LsdException Locked="false" Priority="69" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 3 Accent 5"/> <w:LsdException Locked="false" Priority="70" SemiHidden="false" UnhideWhenUsed="false" Name="Dark List Accent 5"/> <w:LsdException Locked="false" Priority="71" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Shading Accent 5"/> <w:LsdException Locked="false" Priority="72" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful List Accent 5"/> <w:LsdException Locked="false" Priority="73" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Grid Accent 5"/> <w:LsdException Locked="false" Priority="60" SemiHidden="false" UnhideWhenUsed="false" Name="Light Shading Accent 6"/> <w:LsdException Locked="false" Priority="61" SemiHidden="false" UnhideWhenUsed="false" Name="Light List Accent 6"/> <w:LsdException Locked="false" Priority="62" SemiHidden="false" UnhideWhenUsed="false" Name="Light Grid Accent 6"/> <w:LsdException Locked="false" Priority="63" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 1 Accent 6"/> <w:LsdException Locked="false" Priority="64" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 2 Accent 6"/> <w:LsdException Locked="false" Priority="65" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 1 Accent 6"/> <w:LsdException Locked="false" Priority="66" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 2 Accent 6"/> <w:LsdException Locked="false" Priority="67" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 1 Accent 6"/> <w:LsdException Locked="false" Priority="68" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 2 Accent 6"/> <w:LsdException Locked="false" Priority="69" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 3 Accent 6"/> <w:LsdException Locked="false" Priority="70" SemiHidden="false" UnhideWhenUsed="false" Name="Dark List Accent 6"/> <w:LsdException Locked="false" Priority="71" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Shading Accent 6"/> <w:LsdException Locked="false" Priority="72" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful List Accent 6"/> <w:LsdException Locked="false" Priority="73" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Grid Accent 6"/> <w:LsdException Locked="false" Priority="19" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Subtle Emphasis"/> <w:LsdException Locked="false" Priority="21" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Intense Emphasis"/> <w:LsdException Locked="false" Priority="31" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Subtle Reference"/> <w:LsdException Locked="false" Priority="32" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Intense Reference"/> <w:LsdException Locked="false" Priority="33" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Book Title"/> <w:LsdException Locked="false" Priority="37" Name="Bibliography"/> <w:LsdException Locked="false" Priority="39" QFormat="true" Name="TOC Heading"/> </w:LatentStyles> </xml><![endif]--><!-- [if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-qformat:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.5pt; mso-bidi-font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-font-kerning:1.0pt;} </style> <![endif]-->

我们经常用到的控件都是 View 的派生类,他们通常都是可见的。 ViewGroup 也是 View 的派生类,但 ViewGroup 通常是不可见的。

ViewGroup 的主要作用:

+ 作为 Layout 。比如 LinearLayout RelativeLayout FrameLayout TableLayout

+ 作为 View 的容器。比如 Gallery GridView ImageSwitcher ScrollView TabHost ListView

其实 Layout 也可以被认为是 View 的一种容器。

本文仅讨论 ViewGroup 作为 Layout 的常用技巧。

1. LinearLayout

LinearLayout 顾名思义就是线性布局。其子 View 对象要么以 排列,要么以 排列,这取决于其 orientation 属性是 horizontal 还是 vertical 的。

创建一个 Android Project 项目。然后在创建一个 linearlayout.xml ,使其内容如下:

<? xml version = "1.0" encoding = "utf-8" ?>

< LinearLayout

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

android:orientation = "vertical"

android:layout_width = "wrap_content"

android:layout_height = "wrap_content" >

< Button

android:id = "@+id/linearbutton01"

android:layout_width = "wrap_content"

android:layout_height = "wrap_content"

android:text = " 按钮 1"

/>

< Button

android:id = "@+id/linearbutton02"

android:layout_width = "wrap_content"

android:layout_height = "wrap_content"

android:text = " 按钮 2"

/>

< Button

android:id = "@+id/linearbutton01"

android:layout_width = "wrap_content"

android:layout_height = "wrap_content"

android:text = " 按钮 3"

/>

< Button

android:id = "@+id/linearbutton01"

android:layout_width = "wrap_content"

android:layout_height = "wrap_content"

android:text = " 按钮 4"

/>

</ LinearLayout >

Activity 对应的代码中的 setContentView 的参数,改为: R.layout.linearlayout ,运行后得到的结果如下:


如果将 linearlayout.xml 中的 orientation 属性值改为 ”horizontal” ,那么运行后的结果如下:

2. RelativeLayout

RelativeLayout 可以根据子 View 对象彼此之间的位置关系来显示子 View 对象。比如通过 ”above” ”below” ”to the left of” ”to the right of” 其他的子 View 对象来定位。

创建一个布局文件 relativelayout.xml ,使其内容如下:

<? xml version = "1.0" encoding = "utf-8" ?>

< RelativeLayout

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

android:id = "@+id/relativelayout"

android:layout_width = "fill_parent"

android:layout_height = "fill_parent" >

< Button

android:id = "@+id/buttonCenter"

android:layout_width = "wrap_content"

android:layout_height = "wrap_content"

android:text = "Center"

android:layout_centerInParent = "true"

/>

< ImageView

android:id = "@+id/ImageView01"

android:layout_width = "wrap_content"

android:layout_height = "wrap_content"

android:layout_above = "@id/buttonCenter"

android:layout_centerHorizontal = "true"

android:src = "@drawable/icon"

/>

< TextView

android:id = "@+id/textview01"

android:layout_width = "wrap_content"

android:layout_height = "wrap_content"

android:layout_toLeftOf = "@id/buttonCenter"

android:textSize = "20px"

android:text = "Android1"

/>

< TextView

android:id = "@+id/textview02"

android:layout_width = "wrap_content"

android:layout_height = "wrap_content"

android:layout_toLeftOf = "@id/buttonCenter"

android:layout_centerVertical = "true"

android:textSize = "20px"

android:text = "Android2"

/>

< TextView

android:id = "@+id/textview03"

android:layout_width = "wrap_content"

android:layout_height = "wrap_content"

android:layout_below = "@+id/textview01"

android:textSize = "20px"

android:text = "Android3"

/>

</ RelativeLayout >

Activity 对应的代码中的 setContentView 的参数,改为: R.layout.framelayout ,运行后得到的结果如下:

3. FrameLayout

FrameLayout 的形式显示子 View( 比如控件 ) 。可以想 FrameLayout 添加多个 View 对象,但是每个 View 在缺省的情况下都被画在该 layout 的左上角。比如,通常可以用于在同一个区域,显示多个图片。

FrameLayout 的大小就是其中尺寸最大的子 View 的大小。

往项目的 res/drawable-mdpi 文件夹,加入两个图片资源:


它们的文件名分别为: china.png macau.png

创建一个布局文件 framelayout.xml ,使其内容如下:

<? xml version = "1.0" encoding = "utf-8" ?>

< FrameLayout

android:id = "@+id/framelayout"

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

android:layout_width = "wrap_content"

android:layout_height = "wrap_content"

>

< ImageView

android:id = "@+id/imageview01"

android:layout_width = "wrap_content"

android:layout_height = "wrap_content"

android:src = "@drawable/china"

android:minHeight = "200px"

android:minWidth = "200px"

/>

< ImageView

android:id = "@+id/imageview02"

android:layout_width = "wrap_content"

android:layout_height = "wrap_content"

android:src = "@drawable/macau"

android:minHeight = "100px"

android:minWidth = "100px"

android:layout_gravity = "center"

/>

</ FrameLayout >

Activity 对应的代码中的 setContentView 的参数,改为: R.layout.framelayout ,运行后得到的结果如下:


如果把 framelayout.xml 中的第二个 ImageView 中的 android:layout_gravity="center" 属性去掉,那么所得到的结果将是:


如果将 android:layout_gravity="center" 属性加入到 FrameLayout 本身,那么得到的结果如下:


在代码中,我们可以通过形如:

FrameLayout fl = (FrameLayout)this .findViewById(R.id.framelayout );

这样的方式,来获得 FrameLayout 对象,然后通过 FrameLayout 中的方法 addView 向其中增加子 View 对象,也可以通过 removeView 从其中删除子 View 对象。 addView removeView 均有多个重载方法。

4. TableLayout

TableLayout 将子 View 按照 TableRow 的方式从上到下进行排列。 TableRow 基本上和一个 orientation 属性为 “horizontal” LinearLayout 相同,它代表的是一个 TableLayout 中的一行。在 TableRow 中可以增加子 View ,在用一个 TableRow 中子 View 的排列是从左至右的。

缺省地, TableRow 的顺序是从 0 开始的,在一个 TableRow 中的列的顺序也是从 0 开始的。

创建一个 tablelayout.xml ,使其内容如下:

<? xml version = "1.0" encoding = "utf-8" ?>

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

android:id = "@+id/talbelayout"

android:layout_width = "fill_parent"

android:layout_height = "fill_parent"

android:stretchColumns = "*"

>

<!-- * 代表所有的列 也可以用 "0,1,2,3,4..." 代替 -->

< TableRow android:id = "@+id/tablerow0" >

< TextView

android:text = "1234567890"

android:textSize = "10px"

/>

< TextView

android:text = "1234567890"

android:textSize = "10px"

/>

< TextView

android:text = "1234567890"

android:textSize = "10px"

/>

< TextView

android:text = "1234567890"

android:textSize = "10px"

/>

</ TableRow >

<!-- 上面的代码 意味着这个 TableLayout 4 -->

< TableRow android:id = "@+id/tablerow1" >

< Button

android:id = "@+id/leftbutton"

android:text = "Left"

android:textSize = "12px"

android:layout_height = "40px"

/>

< Button

android:id = "@+id/leftbutton"

android:text = "Middle"

android:textSize = "12px"

android:layout_height = "40px"

/>

< Button

android:id = "@+id/leftbutton"

android:text = "Right"

android:textSize = "12px"

android:layout_height = "40px"

android:layout_column = "2"

android:layout_span = "2"

/>

</ TableRow >

<!-- leftbutton 的起始位置在第 2(3) (android:layout_column="2")

占用 2 (android:layout_span="2") -->

< TableRow android:id = "@+id/tablerow2" >

< Button

android:id = "@+id/backbutton"

android:text = "Back Button"

android:layout_column = "2"

/>

</ TableRow >

<!-- backbutton 处于第 2(3) 列的位置 (android:layout_column="2")-->

< TableRow android:id = "@+id/tablerow3" >

< Button

android:id = "@+id/spanbutton"

android:text = "Span Button"

android:layout_column = "0"

android:layout_span = "2"

/>

</ TableRow >

<!-- backbutton 处于第 0(1) 列的位置 (android:layout_column="0")

占用 2 (android:layout_span="2")-->

< TableRow android:id = "@+id/tablerow4" >

< TextView

android:gravity = "right"

android:paddingRight = "8px"

android:text = " 姓名 "

/>

< EditText

android:id = "@+id/editname"

android:layout_column = "1"

android:layout_span = "2"

android:text = ""

/>

</ TableRow >

< TableRow android:id = "@+id/tablerow5" >

< TextView

android:gravity = "right"

android:paddingRight = "8px"

android:text = " 地址 "

/>

< EditText

android:id = "@+id/editname"

android:layout_column = "1"

android:layout_span = "3"

android:text = ""

/>

</ TableRow >

</ TableLayout >

Activity 对应的代码中的 setContentView 的参数,改为: R.layout.tableayout ,运行后得到的结果如下:


分享到:
评论

相关推荐

    androidlayout-marginBottom的值为负数.docx

    xmlns:android="http://schemas.android.com/apk/res/android"&gt;  android:layout_width="fill_parent"  android:layout_height="46.0dip"  android:layout_marginBottom="-1.0dip"  android:src="@drawab

    Android程序技术:常用控件.pptx

    android:layout_width和android:layout_height用于设置控件的宽和高,match_parent表示匹配父容器,wrap_content则是与内容相适应。 android:text用于设置文字的内容 android:textColor用于设置文字的颜色 android:...

    Layout_Gravity

    android:layout_gravity 属性详细解读

    Android购物车代码

    -&lt;LinearLayout android:background="@drawable/aaa" android:weightSum="1" android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" xmlns:android=...

    Android实训购物车页面

    -&lt;LinearLayout android:background="@drawable/aaa" android:weightSum="1" android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" xmlns:android=...

    android自定义弹出框

    &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" &gt; &lt;RelativeLayout xmlns:...

    Get清风android实验2界面设计:基本组件.doc

    编写布局代码,如下 &lt;LinearLayout xmlns:android = android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "vertical"&gt; &lt;!-- 主布局中添加文本框和输入框 --&gt; ...

    android实验2界面设计:基本组件.doc

    西 安 邮 电 大 学 (计算机学院) 课内实验报告 实验名称: 界面设计:基本组件 专 业: 网络工程 班 级: 姓 名: 学 号: 指导教师: 日 期: 2017年4月20日... 编写布局代码,如下 &lt;LinearLayout xmlns:android = ...

    Android属性大全

    android:layout_centerHrizontal 水平居中 android:layout_centerVertical 垂直居中 android:layout_centerInparent 相对于父元素完全居中 android:layout_alignParentBottom 贴紧父元素的下边缘 android:layout_...

    android_QQ_例子

    &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" &gt; android:layout...

    android:layout_gravity和android:gravity的区别

    1.首先来看看Android:layout_gravity和android:gravity的使用区别。 android:gravity: 这个是针对控件里的元素来说的,用来控制元素在该控件里的显示位置。例如,在一个Button按钮控件中设置如下两个属性, android...

    WeChatSample

    xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" ...

    [Android应用开发电子书]

    xmlns:android="http://schemas.android.com/apk/res/android" &gt; android:id="@+id/ImageView1" android:layout_width="wrap_content" android:layout_height="wrap_content"/&gt; android:id="@+id/zoomout" ...

    android中ratingbar的简单使用

    &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" &gt; android:layout_...

    点按弹出按钮组件

    android:layout_width="fill_parent" android:layout_height="fill_parent" zhy:position="left_top" zhy:radius="130dp" &gt; android:layout_width="wrap_content" android:layout_height="wrap_content" ...

    this is a res

    android:layout_below="@+id/displayPhoto" /&gt; android:id="@+id/lastPhoto" android:layout_width="100dp" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_...

    Android 百分比布局

    android:id="@+id/tv_solid_number_layout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="24px" android:layout_weight="1" android:orientation=...

    login_interface

    android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="15dip" android:background="@drawable/background_login" android:padding="15dip" &gt; android:id="@+...

    Android控件大全以及各布局空间的使用方式

    &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:gravity="right" android:layout_width="fill_parent" android:layout_height="fill_parent...

    ANDROID实验报告组件布局.pdf

    Android 开发 (实验五) 实验题目:Android 组件布局试验 指导老师: 班 级:计算机科学与技术系班 姓 名: 一、实验目的 1、掌握 Android 组件布局的使用方法 2、学会组件布局的重要属性与应用 3、能够根据需求,...

Global site tag (gtag.js) - Google Analytics