浅析Android的布局

Android两大布局

线性布局

常用属性

  • orientation:布局中的排列方式有horizon(水平方向),vertical(垂直方向)两种排列方向。
  • gravity:控制组件所包含的元素对齐方式。
  • layout-gravity:控制组件在父容器里面的元素对齐方式。
  • layout-height:布局的高度。
  • layout-weigh:布局的宽度。
  • ID:为组件设置一个标志位,以便于在java文件中通过findviewById找到此组件。
  • background:为组件设置颜色及图片。
<LinearLayout 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:layout_height="match_parent"
    android:orientation="horizontal"
    android:gravity="center"
    tools:context="com.example.zwzz.myapplication.MainActivity">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="点击开始"
        android:id="@+id/bt_ks"
        android:layout_gravity="center"
        android:background="@color/colorAccent"/>
</LinearLayout>

weight(权重)

  • 这个属性是用来按比例划分区域的。
  android:layout_weight="1"

相对布局

基本属性

  • gravity:设置容器内组件对齐方式。
  • ignoreGravity:设置了该属性为true的属性将不受gravity属性的影响。

根据父容器定位

  • layout-alighParentLeft:左对齐。
  • layout-alighParentRight:右对齐。
  • layout-alighParentTop:顶部对齐。
  • layout-alighParentBotoom:底部对齐。
  • android:layout-centerHorizon:水平居中。
  • android:layout-centerVertical:垂直居中。
  • android:layout-centerParent:中间位置。

根据兄弟组件定位

  • layout-toRight:参考组件的右边。
  • layout-toLeft:参考组件的左边。
  • layout-above:参考组件的上方。
  • layout-below:参考组件的下方。
  • layout-alignTop::对齐参考组件的上边界。
  • layout-alignBottom::对齐参考组件的下边界。
  • layout-alignLeft::对齐参考组件的左边界。
  • layout-alignRight::对齐参考组件的右边界。

margin(偏移)

  • layout-margin:设置组件上下左右的偏移量。
  • layout-marginRight:设置组件离右边的偏移量。
  • layout-marginLeft:设置组件离左边的偏移量。
  • layout-marginTop:设置组件离上边的偏移量。
    • layout-marginBotoom:设置组件离下方的偏移量。

padding(填充)

  • layout-padding:往内部元素的上下左右填充一定的边距。
  • layout-paddingRight:往内部元素的右边填充一定的边距。
  • layout-paddingLeft:往内部元素的左边填充一定的边距。
  • layout-paddingTop:往内部元素的上面填充一定的边距。
  • layout-paddingBotoom:往内部元素的下面填充一定的边距。

    举个简单例子

<RelativeLayout 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:layout_height="match_parent"
    android:gravity="center"
    tools:context="com.example.zwzz.myapplication.MainActivity">

    <Button
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:text="点击开始"
        android:id="@+id/bt_ks"
        android:background="@color/colorAccent"/>
    <Button
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:text="点击结束"
        android:id="@+id/bt_js"
        android:background="@color/colorPrimary"
        android:layout_marginLeft="50dp"
        />
    <Button
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:text="还没开始"
        android:id="@+id/bt_hmks"
        android:background="@color/colorPrimaryDark"
        />
    <Button
        android:layout_width="50dp"
        android:layout_height="wrap_content" 
         android:layout_toRightOf="@id/bt_hmks"
        android:layout_marginTop="50dp"
        android:text="还没结束"
        android:background="@color/colorAccent"
        />
</RelativeLayout>

猜你喜欢

转载自blog.csdn.net/Zwousika/article/details/80565421