源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

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

  • 时间:2021-05-26 04:54 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Android中LinearLayout布局的常用属性总结
[b]基本属性要求 [/b]
<LinearLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"

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

[list] [*]android:orientation[/*] [*]决定是水平排列或是垂直排列[/*] [*]vertical 垂直排列[/*] [*]horizontal 水平排列[/*] [/list] [b]垂直排列 Button [/b]
<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>

[b]水平排列 Button [/b]
<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>

[b]重心设定 [/b]
<LinearLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"

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

[list] [*]android:gravity[/*] [*]设定框架的内容的放置方向[/*] [*]center 水平垂直皆置中[/*] [*]center_vertical 垂直置中[/*] [*]center_horizontal 水平置中[/*] [*]top 置顶[/*] [*]left 置左[/*] [*]bottom 置底[/*] [*]right 置右[/*] [/list] [b]水平、垂直置中 [/b]
<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>

[b]透过 OR 运算子组合重心 [/b]
<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>

[b]比例分配 [/b]
<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>

[list] [*]android:layout_weight[/*] [*]子元件或子框架的比重。[/*] [*]LinearLayout 下的子元件或子框架,才能设定这项属性。[/*] [/list] [b]等比例分配 [/b]
<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,所以大小相同。 [b]非等比例分配 [/b]
<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% 分配。      
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部