布局,全屏,横屏竖屏

本文详细介绍了Android中的五大布局:FrameLayout、LinearLayout、AbsoluteLayout、RelativeLayout和TableLayout,并分别阐述了它们的特点和用法。此外,还讲解了如何实现全屏显示以及横屏竖屏切换的两种方法,包括在代码中设置和在AndroidManifest.xml中配置全屏主题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

五大布局

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">
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值