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

相关推荐
Lei活在当下1 天前
【项目踩坑实录】并发环境下,Glide缓存引起的图片加载异常
android·debug·glide
my_power5201 天前
检出git项目到android studio该如何配置
android·git·android studio
三少爷的鞋1 天前
Repository 方法设计:suspend 与 Flow 的决选择指南(以朋友圈为例)
android
阿里云云原生1 天前
Android App 崩溃排查指南:阿里云 RUM 如何让你快速从告警到定位根因?
android·java
cmdch20171 天前
手持机安卓新增推送按钮功能
android
攻城狮20151 天前
【rk3528/rk3518 android14 kernel-6.10 emcp sdk】
android
何妨呀~1 天前
mysql 8服务器实验
android·mysql·adb
QuantumLeap丶1 天前
《Flutter全栈开发实战指南:从零到高级》- 25 -性能优化
android·flutter·ios
木易 士心1 天前
MVC、MVP 与 MVVM:Android 架构演进之路
android·架构·mvc
百锦再1 天前
国产数据库的平替亮点——关系型数据库架构适配
android·java·前端·数据库·sql·算法·数据库架构