当前位置: 移动技术网 > 移动技术>移动开发>Android > Android中LinearLayout布局的常用属性总结

Android中LinearLayout布局的常用属性总结

2019年07月24日  | 移动技术网移动技术  | 我要评论
基本属性要求 <linearlayout android:layout_width="match_parent" android:layou

基本属性要求

<linearlayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"

  android:orientation="vertical">
 </linearlayout>

  • android:orientation
  • 决定是水平排列或是垂直排列
  • vertical 垂直排列
  • horizontal 水平排列

垂直排列 button

<linearlayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >

  <button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="button 1" />
  <button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="button 2" />

</linearlayout>

水平排列 button

<linearlayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >

  <button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="button 1" />
  <button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="button 2" />

</linearlayout>

重心设定

<linearlayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"

  android:gravity="left">
</linearlayout>

  • android:gravity
  • 设定框架的内容的放置方向
  • center 水平垂直皆置中
  • center_vertical 垂直置中
  • center_horizontal 水平置中
  • top 置顶
  • left 置左
  • bottom 置底
  • right 置右

水平、垂直置中

<linearlayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  android:gravity="center_vertical">

  <button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="button 1" />

</linearlayout>

<linearlayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  android:gravity="center_horizontal">

  <button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="button 1" />

</linearlayout>

<linearlayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  android:gravity="center">

  <button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="button 1" />

</linearlayout>

透过 or 运算子组合重心

<linearlayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  android:gravity="top|right">

  <button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="button 1" />

</linearlayout>

<linearlayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  android:gravity="bottom|left">

  <button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="button 1" />

</linearlayout>

<linearlayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  android:gravity="center_vertical|center_horizontal">

  <button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="button 1" />

</linearlayout>

比例分配

<linearlayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="horizontal">

  <button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="button 1"

    android:layout_weight="1"/>

</linearlayout>

  • android:layout_weight
  • 子元件或子框架的比重。
  • linearlayout 下的子元件或子框架,才能设定这项属性。

等比例分配

<linearlayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="horizontal">

  <button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="button 1"
    android:layout_weight="1"/>

  <button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="button 2"
    android:layout_weight="1"/>

  <button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="button 3"
    android:layout_weight="1"/>

</linearlayout>

比重都是 1,所以大小相同。


非等比例分配

<linearlayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="horizontal">

  <button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="button 1"
    android:layout_weight=".10"/>

  <button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="button 2"
    android:layout_weight=".20"/>
  <button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="button 3"
    android:layout_weight=".70"/>

</linearlayout>

.10 代表 0.10
.20 代表 0.20
.70 代表 0.70
合起来刚好是 1 ,作 100% 分配。      

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网