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

相关推荐
ha_lydms5 小时前
AnalyticDB分区、分布键性能优化
android·大数据·分布式·性能优化·分布式计算·分区·analyticdb
星辰6 小时前
Ijkplayer重新编译支持h264裸流
android
测试开发-学习笔记6 小时前
Android studio安装
android·ide·android studio
宋拾壹6 小时前
同时添加多个类目
android·开发语言·javascript
●VON7 小时前
AtomGit Flutter鸿蒙客户端:数据模型
android·服务器·安全·flutter·harmonyos·鸿蒙
火柴就是我7 小时前
记录一个文本随手指缩放的功能
android
Zender Han8 小时前
Android APK 签名 v1、v2、v3、v4 有什么区别?
android
神仙别闹8 小时前
基于 PHP + MySQL学生信息管理系统
android·mysql·php
墨狂之逸才9 小时前
Android 保活机制详解 —— 从概念到实践
android
故渊at9 小时前
第二板块:Android 四大组件标准化学理 | 第十二篇:四大组件全景总结与系统服务(System Server)架构
android·架构·wpf·四大组件·system service