(一)Android布局之线性布局(LinearLayout)

向右看齐 2022-08-06 14:17 441阅读 0赞

Android在主窗口列出的布局类型有四种,分别是FrameLayout(帧布局),LinearLayout(线性布局),TableLayout(表格布局),RelativeLayout(相对布局)。LinearLayout(线性布局)会根据其orientation属性的值来决定是按行还是按列逐个显示。

示例main.xml布局文件如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. >
  7. <TextView
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="@string/name_text"/>
  11. <EditText
  12. android:layout_width="fill_parent"
  13. android:layout_height="wrap_content" />
  14. <Button
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:text="@string/cancle_button"/>
  18. <Button
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"
  21. android:text="@string/ok_button"/>
  22. </LinearLayout>

“Layout_width”指定该元素的宽度,可选值有四种,“fill_parent“,“wrap_content”,“math_parent”和具体的数字(单位为dp,px)。其中“fill_parent”代表填满其父元素,对于顶级元素来说,其父元素就是整个手机屏幕;“wrap_content”代表该元素的大小仅包含其自身内容,”match_parent”表示匹配整个上层组件,而数字仅代表占相应的px;“Layout_height”代表该元素的高度,可选参数值与“Layout_width”的参数意义相同。

对应string.xml的内容如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <string name="app_name">用户界面</string>
  4. <string name="hello_world">Hello world!</string>
  5. <string name="action_settings">Settings</string>
  6. <string name="name_text">请输入用户名</string>
  7. <string name="ok_button">确定</string>
  8. <string name="cancle_button">取消</string>
  9. </resources>

属性“xmlns:android”指定命名空间,顶级元素必须指定命名空间。属性“orientation”指定子元素排列方式,其中指定为“vertical”则是子元素垂直排列,每个子元素会占独立的一行,而另一个可选值为“horizontal”,代表子元素水平排列,即每个子元素会占独立的一列。

效果如图所示:

Center

Center 1

发表评论

表情:
评论列表 (有 0 条评论,441人围观)

还没有评论,来说两句吧...

相关阅读