加入收藏 | 设为首页 | 会员中心 | 我要投稿 云计算网_泰州站长网 (http://www.0523zz.com/)- 视觉智能、AI应用、CDN、行业物联网、智能数字人!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

Android开发:Layout常用方法

发布时间:2021-12-22 12:48:56 所属栏目:PHP教程 来源:互联网
导读:我们经常用到的控件都是 View 的派生类,他们通常都是可见的。 ViewGroup 也是 View 的派生类,但 ViewGroup 通常是不可见的。 ViewGroup 的主要作用: + 作为 Layout 。比如 LinearLayout 、 RelativeLayout 、 FrameLayout 和 TableLayout + 作为 View 的

我们经常用到的控件都是 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 

(编辑:云计算网_泰州站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读