Android布局(RelativeLayout、TableLayout等)使用方法

2019-12-10 18:51:19于丽
易采站长站为您分析Android布局使用方法及各种属性介绍,包括RelativeLayout、TableLayout等,感兴趣的朋友可以参考一下  

 本文介绍 Android 界面开发中最基本的四种布局LinearLayout、RelativeLayout、FrameLayout、TableLayout 的使用方法及这四种布局中常用的属性。

  • LinearLayout 线性布局,布局中空间呈线性排列
  • RelativeLayout 相对布局,通过相对定位的方式,控制控件位置
  • FrameLayout 帧布局,最简单的布局,所有控件放置左上角
  • TableLayout 表格布局,以行列方式控制控件位置

    四种布局示例

    1.LinearLayout

    <LinearLayout xmlns:android="http://www.easck.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical">
     
      <LinearLayout xmlns:android="http://www.easck.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:orientation="vertical">
     
        <Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="垂直1" />
        <Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="垂直2" />
      </LinearLayout>
     
      <LinearLayout xmlns:android="http://www.easck.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:orientation="horizontal">
     
        <Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="水平1" />
        <Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="水平2" />
      </LinearLayout>
     
      <LinearLayout xmlns:android="http://www.easck.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:orientation="horizontal">
     
        <Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="top"
          android:text="水平上对齐" />
        <Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="center_vertical"
          android:text="水平垂直居中" />
        <Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="bottom"
          android:text="水平下对齐" />
      </LinearLayout>
     
      <LinearLayout xmlns:android="http://www.easck.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:orientation="horizontal">
        <EditText
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:layout_weight="3"
          android:hint="请输入..."/>
        <Button
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:layout_weight="2"
          android:text="提交" />
      </LinearLayout>
     
      <LinearLayout xmlns:android="http://www.easck.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:orientation="horizontal">
        <EditText
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:hint="请输入..."/>
        <Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="提交" />
      </LinearLayout>
    </LinearLayout>