Android3:布局

一。线性布局

创建项目Linear Layout Example

activity_main.xml

复制代码
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              xmlns:tools="http://schemas.android.com/tools"
              android:gravity="center"
              android:orientation="vertical">

    <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="To"
            android:inputType="text" />

    <EditText
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:gravity="top"
            android:hint="Message"
            android:inputType="textMultiLine" />

    <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end"
            android:layout_marginTop="40dp"
            android:text="Send" />

</LinearLayout>

二。帧布局

创建项目Frame Layout Example

activity_main.xml

复制代码
<?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"
              xmlns:tools="http://schemas.android.com/tools">

    <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scaleType="centerCrop"
            android:src="@drawable/duck"
            android:contentDescription="Duck image" />

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            android:text="It's a duck!" />

</FrameLayout>

为项目添加图片duck.webp
app/src/main/res/drawable/duck.webp

三。滚动布局

创建项目Scroll View Example

activity_main.xml

复制代码
<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             xmlns:tools="http://schemas.android.com/tools"
             android:fillViewport="true">

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:padding="16dp" >

        <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Message"
                android:inputType="textMultiLine" />

        <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Send" />
    </LinearLayout>

</ScrollView>

知识点:

android:layout_weight:视图权重
android:layout_gravity:视图位置
android:fillViewport:视图是否要填满屏幕,当内容超过屏幕大小时会出现滚动条,可以往下滚动

相关推荐
懒人村杂货铺2 小时前
Android BLE 扫描完整实战
android
TeleostNaCl4 小时前
如何安装 Google 通用的驱动以便使用 ADB 和 Fastboot 调试(Bootloader)设备
android·经验分享·adb·android studio·android-studio·android runtime
fatiaozhang95275 小时前
中国移动浪潮云电脑CD1000-系统全分区备份包-可瑞芯微工具刷机-可救砖
android·网络·电脑·电视盒子·刷机固件·机顶盒刷机
2501_915918416 小时前
iOS 开发全流程实战 基于 uni-app 的 iOS 应用开发、打包、测试与上架流程详解
android·ios·小程序·https·uni-app·iphone·webview
lichong9516 小时前
【混合开发】vue+Android、iPhone、鸿蒙、win、macOS、Linux之dist打包发布在Android工程asserts里
android·vue.js·iphone
Android出海6 小时前
Android 15重磅升级:16KB内存页机制详解与适配指南
android·人工智能·新媒体运营·产品运营·内容运营
一只修仙的猿6 小时前
毕业三年后,我离职了
android·面试
编程乐学7 小时前
安卓非原创--基于Android Studio 实现的新闻App
android·ide·android studio·移动端开发·安卓大作业·新闻app
雅雅姐8 小时前
Android14 init.rc中on boot阶段操作4
android
fatiaozhang95278 小时前
中国移动中兴云电脑W132D-RK3528-2+32G-刷机固件包(非原机制作)
android·xml·电脑·电视盒子·刷机固件·机顶盒刷机