3.12学习总结

  今天将之前的布局复习了一下,虽然LinearLayout一直在用,但是具体是什么我还不知道,今天就学习一下。

  

1. 线性布局(LinearLayout)
线性布局就是将一些控件排放在一条线上,但是有水平方向和垂直方向两种。水平和垂直方向的控制由属性android:orientation来控制,这个属性有两个值:垂直(Vertical)和水平(Horizaontal),在线性布局中还有一些比较常用的属性,如:android:gravity、android:weight(控件的权值)等。
代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#ffffff"
>
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:orientation="horizontal"
>
<Button
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:text="按钮1"
android:layout_weight="1.0"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:text="按钮2"
android:layout_weight="1.0"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:text="按钮3"
android:layout_weight="1.0"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="按钮4"
android:layout_weight="1.0"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="按钮5"
android:layout_weight="1.0"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="按钮6"
android:layout_weight="1.0"
/>
</LinearLayout>
</LinearLayout>

运行效果:

 

 

2.相对布局(RelativelLayout)

相对布局是指按照控件间的相对位置进行布局,也就是说我们可以选一个控件作为参照,其他的控件可以在它的上边、下边、左边及右边等。


 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#ffffff"
>
<TextView
android:id="@+id/textView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AA"
android:textColor="#00FF00"
android:textSize="22sp"
android:layout_marginLeft="20px"/>

<TextView
android:id="@+id/textRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/textView01"
android:text="BB"
android:textColor="#FF0000"
android:textSize="22sp"
android:layout_marginLeft="20px"/>
<TextView
android:id="@+id/text02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView01"
android:text="CC"
android:textColor="#00FFFF"
android:textSize="22sp"
android:layout_marginLeft="20px"/>
<TextView
android:id="@+id/textView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textRight"
android:layout_toRightOf="@id/text02"
android:text="DD"
android:textColor="#000000"
android:textSize="22sp"
android:layout_marginLeft="20dip"/>

</RelativeLayout>

运行结果如下:
BB相对于AA的左边,CC相对于AA的下边边,DD相对于BB的下边且相对于CC的右边

3.表格布局(TableLayout)
表格布局适用于N行N列的布局格式。一个TableLayout由许多TableRow组成,一个TableRow就代表TableLayout中的一行。TableRow的数量决定表格的行数。而表格的列数是由包含最多控件的TableRow决定。

 

  

 

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:stretchColumns="1"
>
<TableRow>
<TextView
android:layout_column = "1"
android:padding="3dip"
android:textColor="#000000"
android:text="open..."/>
<TextView
android:gravity="right"
android:padding="3dip"
android:textColor="#000000"
android:text="Ctrl_o"/>
</TableRow>

<View
android:layout_height="2dip"
android:background="#FF909090"/>

<TableRow>
<TextView
android:layout_column = "1"
android:padding="3dip"
android:textColor="#000000"
android:text="Save..."/>
<TextView
android:gravity="right"
android:padding="3dip"
android:textColor="#000000"
android:text="Ctrl_s"/>
/>
</TableRow>

<View
android:layout_height="2dip"
android:background="#FF909090"/>

<TableRow>
<TextView
android:layout_column = "1"
android:padding="3dip"
android:textColor="#000000"
android:text="Save As..."/>
<TextView
android:gravity="right"
android:padding="3dip"
android:textColor="#000000"
android:text="Ctrl_shift_s"/>
</TableRow>

<View
android:layout_height="2dip"
android:background="#FF909090"/>

<TableRow>
<TextView
android:layout_column = "1"
android:padding="3dip"
android:textColor="#000000"
android:text="X"/>
<TextView  
android:gravity="right"
android:padding="3dip"
android:textColor="#000000"
android:text="Import..."/>
</TableRow>
</TableLayout>

运行结果如下:

  

  布局还有绝对布局以及帧布局,因使用频率较少,故不再展示,需要时网上查即可。

posted @   Joranger  阅读(4)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
点击右上角即可分享
微信分享提示