1.LinearLayout线性布局:在线性方向上依次排列(一横排或一竖排)
<LinearLayout.....
android:orientation="vertical">
<button1>
<button2>
<button3>
</LinearLayout>
通过设置orientation属性来指定线型排列的方向:vertical表示的是垂直方向、horizontal表示的是水平方向。
若果不指定orientation的值 则默认为horizontal。
注:如果排列方向为horizontal,则不能设置控件layout_width宽度属性为catch_parent,这样的话单独一个空间就占据了整行。
排列方向为vertical,则不能设置控件layout_height高度属性为catch_parent。
1)<android:layout_gravity="参数">用于控制控件在布局中的对齐方式,参数值可写为top、center、bottom等
需要注意的是当排列方式为horizontal时只能指定垂直方向上的对齐方式,反之当排列方式为vertical时,只能指定水平方向上的对齐方式。
2)<android:laoyout_weight="1">该属性允许使用比例的方式来指定空间的大小:
<EditText
android:layout_width="0dp"
android"layout_weight="1"
/>
<button
android:layout_width="0dp"
android"layout_weight="1"
/>
此时两控件的宽度属性不由wight属性决定,weight属性值均为1表示在水平方向上两控件平分宽度。======================================================================================================
2.RelativeLayout相对布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:id="@+id/butoon1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="喜羊羊"
/>
</RelativeLayout >
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
两行代码共同决定了控件位置是 屏幕右上方同理 右下方、左上方、左下方
以上所说的位置是相对于屏幕来说
android:layout_centerInParent="true"
控件位置是屏幕中心位置
<RelativeLayout >
<Button
android:id="@+id/butoon1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="button1"
/>
<Button
android:id="@+id/butoon2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/butoon1"
android:layout_toLeftOf="@id/butoon1"
android:text="喜羊羊"
/>
</RelativeLayout >
android:layout_above这个属性可以让控件位于另一个控件的上方,需要指定相对控件的id,上述代码可以理解为button2位于button1的上方
android:layout_below表示这个控件位于另一个控件的下方
android:layout_toLeftOf表示一个控件位于另一个控件的左侧.
android:layout_toRightOf表示一个控件位于另一个控件的右侧
配合使用可以达到控件位于另一个控件的左上、左下、右上、右下、的效果
注意:当一个空间去引用另一个控件的id时该控件一定要定义于引用控件的后面,不然会出现找不到id的情况
android:layout_alignLeft表示一个空间的左边缘和另一个控件的左边缘对齐
======================================================================================================
3.FrameLayout:所有的控件都会摆在父布局的左上角
======================================================================================================
4.TableLayout:允许使用表格的方式排列控件
<TableLayout
android:stretchColumns="1">
<TableRow >
<TextView
android:id="@+id/textview"
android:layout_height="wrap_content"
android:text="账号"
/>
<EditText
android:id="@+id/edittext"
android:layout_height="wrap_content"
android:hint="请输入账号"
/>
</TableRow>
<TableRow >
<Button
android:id="@+id/button"
android:layout_height="wrap_content"
android:layout_span="2"
android:text="点击登陆"
/>
</TableRow>
</TableLayout >
<TableRow > </TableRow > :在TableLayout中每添加一个TableRow就添加了一行,在TableRow中每添加一个控件就添加了一列,TableRow中的控件都不能指定layout_width宽度属性
android:layout_span="2" : 该属性可以使登陆按钮占据两个列
android:stretchColumns="1"表示如果表格宽度不能占满屏幕,将第二列进行拉伸,若果属性值改为0,则将第一列进行拉伸
====================================================================================================================================5.自定义布局
实际工作当中,一部分代码几乎每个界面都会用到,如果每个界面都要重新写的话,会造成代码量的重复和繁琐得工作,所以我们可以通过自定义布局,将一部分每个界面都会用的布局写入一个XML文件中,命名为title.xml,然后在activity_main.xml文件中写入以下代码:<include layout="@layout/title"/> 即可实现自定义界面布局的导入。
======================================================================================================6.自定义控件
自定义控件的原理和自定义布局差不多,都可以减少重复的代码,比如说和多洁面中都会用到Back按键销毁当前活动,这样我们就可以把这个button按钮封装好 然后哪里需要就引入哪里,代码如下:
public class TitleLayout extends LinearLayout{ //新建一个类继承自LinearLayout
public TitleLayout(Context context,AttributeSet attrs) { //重写LinearLayout中的Titlelayout方法在布局中引入TitleLayout就会调用这个方法
super(context,attrs);
LayoutInflater.from(context).inflate(R.layout.title, this); //layoutInflater的from()方法可以加载一个布局文件,inflate(a,b)中有两个参个 数,a参数表示要加载的布局文件的Id;b参数表示给加载好的布局添加一个父布局
Button button1 = (Button) findViewById(R.id.button1); //此处定义了两个butto类型的按钮对象
Button button2=(Button) findViewById(R.id.button2);
button1.setOnClickListener(new OnClickListener(){ //给按钮button1添加一个监听事件即点击button1会执行的动作
@Override
public void onClick(View v) { //OnClick方法表示点击后执行的动作
((Activity) getContext()).finish(); //这行代码表示点击butto1后页面销毁了
}
});
button2.setOnClickListener(new OnClickListener(){ //给button2添加的一个监听事件
@Override
public void onClick(View v) {
Toast toast = Toast.makeText(getContext(), "Duang", Toast.LENGTH_SHORT); //这行代码的意思是点击button2后会屏幕显示一个文本框
Toast.makeText(a,b,c);a参数表示当前的上下文环境;b参数
表示显 示的内容;c参数表示文本框显示的时间分为SHORT和LONG;
}
});
}
}