五大布局
FrameLayout(框架布局),LinearLayout (线性布局),AbsoluteLayout(绝对布局),RelativeLayout(相对布局),TableLayout(表格布局)。
1、FrameLayout
所有东西依次都放在左上角,会重叠,这个布局比较简单,也只能放一点比较简单的东西。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0b0">
<TextView
android:layout_width="300dp"
android:layout_height="300dp"
android:background="#021"
android:layout_gravity="center" />
<TextView
android:layout_width="200dp"
android:layout_height="200dp"
android:background="#d31"
android:layout_gravity="center" />
<TextView
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#02f"
android:layout_gravity="center" />
<TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#fff"
android:layout_gravity="center" />
</FrameLayout>
2、 LinearLayout
每一个LinearLayout里面又可分为垂直布局(android:orientation=”vertical”)和水平布局(android:orientation=”horizontal” )。当垂直布局时,每一行就只有一个元素,多个元素依次垂直往下;水平布局时,只有一行,每一个元素依次向右排列。
3、 AbsoluteLayout
绝对布局用X,Y坐标来指定元素的位置,这种布局方式也比较简单,但是在屏幕旋转时,往往会出问题,而且多个元素的时候,计算比较麻烦。
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#f00"
android:layout_x="70dp"//离开左上角的横竖距离
android:layout_y="70dp"/>
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#f00"
android:layout_x="80dp"
android:layout_y="80dp"/>
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#f00"
android:layout_x="90dp"
android:layout_y="90dp"/>
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#f00"
android:layout_x="100dp"
android:layout_y="100dp"/>
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#f00"
android:layout_x="110dp"
android:layout_y="110dp"/>
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#f00"
android:layout_x="120dp"
android:layout_y="120dp"/>
</AbsoluteLayout>
4、 RelativeLayout
相对布局可以理解为某一个元素为参照物,来定位的布局方式。主要属性有:相对于某一个元素android:layout_below、 android:layout_toLeftOf相对于父元素的地方android:layout_alignParentLeft、android:layout_alignParentRigh
5、 TableLayout
每一个TableLayout里面有表格行TableRow,TableRow里面可以具体定义每一个元素。
TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:collapseColumns=""
android:shrinkColumns="1"
android:stretchColumns="0,2"
>
<TableRow>
<Button android:text="你"/>
<Button android:text="真"/>
<Button android:text="行"/>
</TableRow>
<TableRow>
<Button android:text="我"/>
<Button android:text="服了服了服了服了服了服了"/>
<Button android:text="U"/>
</TableRow>
每一个布局都有自己适合的方式,这五个布局元素可以相互嵌套应用,做出美观的界面。
全屏,横屏竖屏
1、全屏
在MainActivity的onCreate方法中添加代码
this.requestWindowFeature(Window.FEATURE_NO_TITLE); this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
或者直接在AndroidManifest.xml文件中设定Activity主题为全屏模式
android:theme=”@android:style/Theme.NoTitleBar.Fullscreen”
2、横屏竖屏
按照下面代码示例修改Activity的onResume方法
@Override
protected void onResume() {
/**
* 设置为横屏竖屏
*/
if(getRequestedOrientation()!=ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//横屏
}
super.onResume();
}
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//竖屏
//或者在配置文件中对Activity节点添加android:screenOrientation属性(landscape是横向,portrait是纵向)
android:launchMode="singleTask" android:screenOrientation="portrait">