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:视图是否要填满屏幕,当内容超过屏幕大小时会出现滚动条,可以往下滚动

相关推荐
ajassi200010 分钟前
开源 java android app 开发(十八)最新编译器Android Studio 2025.1.3.7
android·java·开源
用户20187928316718 分钟前
Java 泛型:快递站老板的 "类型魔法" 故事
android
Knight_AL32 分钟前
浅拷贝与深拷贝详解:概念、代码示例与后端应用场景
android·java·开发语言
夜晚中的人海2 小时前
【C++】智能指针介绍
android·java·c++
用户2018792831672 小时前
后台Activity输入分发超时ANR分析(无焦点窗口)
android
用户2018792831672 小时前
Activity配置变化后ViewModel 的 “不死之谜”
android
游戏开发爱好者83 小时前
BShare HTTPS 集成与排查实战,从 SDK 接入到 iOS 真机调试(bshare https、签名、回调、抓包)
android·ios·小程序·https·uni-app·iphone·webview
2501_916008893 小时前
iOS 26 系统流畅度实战指南|流畅体验检测|滑动顺畅对比
android·macos·ios·小程序·uni-app·cocoa·iphone
2501_915106325 小时前
苹果软件加固与 iOS App 混淆完整指南,IPA 文件加密、无源码混淆与代码保护实战
android·ios·小程序·https·uni-app·iphone·webview
2501_915921435 小时前
iOS 26 崩溃日志解析,新版系统下崩溃获取与诊断策略
android·ios·小程序·uni-app·cocoa·iphone·策略模式