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

相关推荐
mmsx32 分钟前
Android APK 加固方案怎么落地:从加壳到校验的组合取舍
android·网络·安全·签名
hunterandroid1 小时前
Navigation Component 工程落地:从单 Activity 架构到深度链接配置
android·前端
撩得Android一次心动1 小时前
Jetpack Compose 知识点整理3【个人用】
android·笔记·学习·kotlin·android compose
Kapaseker2 小时前
别再只会写 <T>:Kotlin 泛型的实战用法
android·kotlin
FlightYe2 小时前
音视频修炼之容器与协议(一):MP4和MOV的box剖析
android·linux·c++·ffmpeg·音视频
Vitaliy Kostrov(柯维远)4 小时前
客户端如何检查直播流:从流内标记到停止播放
android·运维·服务器·驱动开发·网络安全·系统架构·cdn
恋猫de小郭4 小时前
Dart 3.15 新特性:this 终于支持类型提升了
android·前端·flutter
ii_best5 小时前
ios手机开发安卓脚本开发软件[按键精灵教程]从「找最小值」到「Boss 最近刷新」再到「屏幕范围极值」
android·ios·智能手机·自动化·按键精灵
开开心心就好5 小时前
安卓PDF处理工具,免费无广告零弹窗
android·服务器·开发语言·算法·pdf·ocr
墨狂之逸才5 小时前
扫码枪响了,应用却没收到:一次 Android USB 线材问题排查
android