[Android新手学习笔记18]-LinearLayou线性布局

  
  
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.    android:orientation="vertical"
  4.    android:layout_width="match_parent"
  5.    android:layout_height="match_parent">
  6.    <Button
  7.        android:id="@+id/button1"
  8.        android:text="Button 1"
  9.        android:textAllCaps="false"
  10.        android:layout_width="wrap_content"
  11.        android:layout_height="wrap_content" />
  12. <Button
  13.        android:id="@+id/button2"
  14.        android:text="Button 1"
  15.        android:textAllCaps="false"
  16.        android:layout_width="wrap_content"
  17.        android:layout_height="wrap_content" />
  18.    <Button
  19.        android:id="@+id/button3"
  20.        android:text="Button 1"
  21.        android:textAllCaps="false"
  22.        android:layout_width="wrap_content"
  23.        android:layout_height="wrap_content" />
  24. </LinearLayout>

android:orientation值为vertical或者horizontal,表示垂直或者水平。

android:layout_gravity表示对齐方式,当android:orientation为vertical时,则android:layout_gravity只有表示水平方向的值才会生效;当android:orientation为horizontal时,则android:layout_gravity只有表示垂直方向的值才会生效。

当android:orientation为vertical时,不能将控件的高度指定为match_parent;当android:orientation为horizontal时,不能将控件的宽度指定为match_parent;

按占比:

  
  
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.    android:orientation="horizontal"
  4.    android:layout_width="match_parent"
  5.    android:layout_height="match_parent">
  6.    <EditText
  7.        android:id="@+id/edit_text"
  8.        android:hint="在这输入内容"
  9.        android:layout_width="0dp"
  10.        android:layout_weight="1"
  11.        android:layout_height="wrap_content" />
  12.    <Button
  13.        android:id="@+id/button1"
  14.        android:text="Button 1"
  15.        android:textAllCaps="false"
  16.        android:layout_width="0dp"
  17.        android:layout_weight="1"
  18.        android:layout_height="wrap_content" />
  19. </LinearLayout>

注意的是一个是:

1.android:layout_width="0dp"

2.android:layout_weigh="1"

计算方式是,把所有的androud:layout_weight的值相加,这里是1+1=2,然后每个控件的宽度计算为:EditText.width=1/2,Button.width=1/2。

使用下面方式,可以让EditText充满剩余空间:

  
  
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.    android:orientation="horizontal"
  4.    android:layout_width="match_parent"
  5.    android:layout_height="match_parent">
  6.    <EditText
  7.        android:id="@+id/edit_text"
  8.        android:hint="在这输入内容"
  9.        android:layout_width="0dp"
  10.        android:layout_weight="1"
  11.        android:layout_height="wrap_content" />
  12.    <Button
  13.        android:id="@+id/button1"
  14.        android:text="Button 1"
  15.        android:textAllCaps="false"
  16.        android:layout_width="wrap_content"
  17.        android:layout_height="wrap_content" />
  18. </LinearLayout>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值