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

来源:互联网 发布:淘宝费用预算 编辑:程序博客网 时间:2024/06/06 12:31
  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
原创粉丝点击