<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/button1" android:text="Button 1" android:textAllCaps="false" android:layout_width="wrap_content" android:layout_height="wrap_content" /><Button android:id="@+id/button2" android:text="Button 1" android:textAllCaps="false" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/button3" android:text="Button 1" android:textAllCaps="false" android:layout_width="wrap_content" android:layout_height="wrap_content" /></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;
按占比:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <EditText android:id="@+id/edit_text" android:hint="在这输入内容" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" /> <Button android:id="@+id/button1" android:text="Button 1" android:textAllCaps="false" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" /></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充满剩余空间:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <EditText android:id="@+id/edit_text" android:hint="在这输入内容" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" /> <Button android:id="@+id/button1" android:text="Button 1" android:textAllCaps="false" android:layout_width="wrap_content" android:layout_height="wrap_content" /></LinearLayout>
新闻热点
疑难解答