Android开发
5、控件的布局方法:
线性布局的使用方法
线性方向:水平,通过设置控件的android:layout_weight属性来设置控件与控件之间,在宽度上的权重比例大小,这里就将EditText控件与Button控件的宽度比例设置为1:1,前提是必须先将这两个控件的android:layout_width="0dp".
代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<EditText
android:id="@+id/et1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Type something" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="send"
android:textAllCaps="false" />
</LinearLayout>
效果图:
如果只是将EditText控件的
android:layout_width="0dp"
android:layout_weight="1"
而Button控件的
android:layout_width="wrap_content“”
android:layout_weight属性不设置(注意这里没设置了)
代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<EditText
android:id="@+id/et1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Type something" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="send"
android:textAllCaps="false" />
</LinearLayout>
效果图: