Androidx学习笔记(6)--常见布局--线性布局

来源:互联网 发布:flash as2.0与js通信 编辑:程序博客网 时间:2024/06/06 13:51

线性布局

LinearLayout
  • 指定各个节点的排列方向

    android:orientation="horizontal"
  • 设置右对齐

    android:layout_gravity="right"
  • 当竖直布局时,只能左右对齐和水平居中,顶部底部对齐竖直居中无效
  • 当水平布局时,只能顶部底部对齐和竖直居中
  • 使用match_parent时注意不要把其他组件顶出去
  • 线性布局非常重要的一个属性:权重

    android:layout_weight="1"


权重设置的是按比例分配剩余的空间,并且要设置layout_width或layout_height要为0
  • 有一个布局方向,水平或者竖直
  • 在竖直布局下,左对齐、右对齐,水平居中生效
  • 在水平布局下,顶部对齐、底部对齐、竖直居中生效
  • 权重:按比例分配屏幕的剩余宽度或者高度

示例:



布局代码

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >        <LinearLayout         android:layout_weight="1"        android:layout_width="match_parent"        android:layout_height="0dp"        android:orientation="horizontal"        >        <TextView             android:layout_weight="1"            android:layout_width="0dp"            android:layout_height="match_parent"            android:background="#ff0000"            />        <TextView             android:layout_weight="1"            android:layout_width="0dp"            android:layout_height="match_parent"            android:background="#ffffff"            />        <TextView             android:layout_weight="1"            android:layout_width="0dp"            android:layout_height="match_parent"            android:background="#000000"            />        <TextView             android:layout_weight="1"            android:layout_width="0dp"            android:layout_height="match_parent"            android:background="@android:color/darker_gray"            />    </LinearLayout>    <LinearLayout         android:layout_weight="1"        android:layout_width="match_parent"        android:layout_height="0dp"        android:orientation="vertical"        >        <TextView             android:layout_weight="1"            android:layout_width="match_parent"            android:layout_height="0dp"            android:background="#00ff00"            />        <TextView             android:layout_weight="1"            android:layout_width="match_parent"            android:layout_height="0dp"            android:background="@android:color/darker_gray"            />        <TextView             android:layout_weight="1"            android:layout_width="match_parent"            android:layout_height="0dp"            android:background="#000000"            />        <TextView             android:layout_weight="1"            android:layout_width="match_parent"                        android:layout_height="0dp"            android:background="#ffcc0000"            />    </LinearLayout></LinearLayout>


0 0
原创粉丝点击