(个人经验,仅供参考,错误之处,敬请谅解)
布局管理器
这里只是大体介绍与给出示例,具体布局所需要用到的详细属性尚未列出。
- 线性布局(LinearLayout)
主要使用orientation 属性(horizontal、vertical)以及多层的嵌套方式来实现对页面的布局
示例:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送短信"
android:layout_gravity="center_horizontal"
android:textSize="30sp"
/>
<LinearLayout android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content">
<TextView
android:text="收件人"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText android:layout_width="wrap_content"
android:hint="收件人电话"
android:layout_weight="2"
android:layout_height="wrap_content"/>
</LinearLayout>
<EditText android:layout_width="match_parent"
android:hint="短信内容"
android:gravity="top"
android:layout_weight="1"
android:layout_height="wrap_content"/>
<LinearLayout android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content">
<Button android:layout_width="wrap_content"
android:text="发送"
android:layout_weight="1"
android:layout_height="wrap_content"/>
<Button android:layout_width="wrap_content"
android:text="取消"
android:layout_weight="1"
android:layout_height="wrap_content"/>
<Button android:layout_width="wrap_content"
android:text="清除"
android:layout_weight="1"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
- 相对布局(RelativeLayout)
通过设置不同组件之间的相对位置来进行布局(分为父子级、同级)
示例:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送短信"
android:textSize="30sp"
android:id="@+id/v1"
/>
<TextView
android:layout_below="@id/v1"
android:text="收件人"
android:id="@+id/v2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText android:layout_width="match_parent"
android:hint="收件人电话"
android:layout_below="@id/v1"
android:layout_toRightOf="@id/v2"
android:id="@+id/v3"
android:layout_height="wrap_content"/>
<EditText android:layout_width="match_parent"
android:hint="短信内容"
android:gravity="top"
android:layout_above="@+id/v4"
android:layout_below="@id/v3"
android:layout_height="wrap_content"/>
<Button android:layout_width="wrap_content"
android:text="发送"
android:id="@+id/v4"
android:layout_toLeftOf="@+id/v5"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content"/>
<Button android:layout_width="wrap_content"
android:text="取消"
android:id="@+id/v5"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content"/>
<Button android:layout_width="wrap_content"
android:text="清除"
android:layout_toRightOf="@id/v5"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content"/>
</RelativeLayout>
- 表格布局(TableLayout)
通过使用表格型对页面进行布局(使用行列)
示例:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:stretchColumns="*"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送短信"
android:textSize="30sp"
/>
<TableRow>
<TextView
android:text="收件人"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText android:layout_width="wrap_content"
android:hint="收件人电话"
android:layout_span="2"
android:layout_height="wrap_content"/>
</TableRow>
<EditText android:layout_width="match_parent"
android:hint="短信内容"
android:layout_weight="1"
android:gravity="top"
android:layout_height="wrap_content"/>
<TableRow>
<Button android:layout_width="wrap_content"
android:text="发送"
android:layout_height="wrap_content"/>
<Button android:layout_width="wrap_content"
android:text="取消"
android:layout_height="wrap_content"/>
<Button android:layout_width="wrap_content"
android:text="清除"
android:layout_height="wrap_content"/>
</TableRow>
</TableLayout>
- 帧布局(FrameLayout)
从页面左上角开始布局,主要使用空白元素(margin属性)
示例:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送短信"
android:layout_marginTop="20dp"
android:textSize="30sp"
/>
<TextView
android:text="收件人"
android:layout_marginTop="40dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText android:layout_width="match_parent"
android:hint="收件人电话"
android:layout_marginTop="40dp"
android:layout_marginLeft="50dp"
android:layout_height="wrap_content"/>
<EditText android:layout_width="match_parent"
android:hint="短信内容"
android:layout_marginTop="80dp"
android:layout_marginBottom="40dp"
android:gravity="top"
android:layout_height="match_parent"/>
<Button android:layout_width="wrap_content"
android:text="发送"
android:layout_gravity="bottom"
android:layout_height="wrap_content"/>
<Button android:layout_width="wrap_content"
android:text="取消"
android:layout_gravity="bottom|center_horizontal"
android:layout_height="wrap_content"/>
<Button android:layout_width="wrap_content"
android:text="清除"
android:layout_gravity="bottom|right"
android:layout_height="wrap_content"/>
</FrameLayout>
- 网格布局(GridLayout)
与表格布局类似,不同点在于可以跨行(rowspan)
【复用布局】
通过写好样式文件(.xml)再进行引用:
<include layout="@layout/file_name"/>
3163

被折叠的 条评论
为什么被折叠?



