视图的边距和填充有什么不同?

推荐答案

为了帮助我记住padding的含义,我想到了一件有很多thick cotton padding的大衣.我在我的外套里,但是我和我的棉衣在一起.我们是一个整体.

但是要记住margin,我想到的是"Hey, give me some margin!",它是我和你之间的空白.别进入我的舒适区--我的边际.

为了更清楚地说明这一点,下面是一张TextView中填充和边距的图片:

enter image description here

上图的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"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="#c5e1b0"
        android:textColor="#000000"
        android:text="TextView margin only"
        android:textSize="20sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="#f6c0c0"
        android:textColor="#000000"
        android:text="TextView margin only"
        android:textSize="20sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#c5e1b0"
        android:padding="10dp"
        android:textColor="#000000"
        android:text="TextView padding only"
        android:textSize="20sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#f6c0c0"
        android:padding="10dp"
        android:textColor="#000000"
        android:text="TextView padding only"
        android:textSize="20sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="#c5e1b0"
        android:textColor="#000000"
        android:padding="10dp"
        android:text="TextView padding and margin"
        android:textSize="20sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="#f6c0c0"
        android:textColor="#000000"
        android:padding="10dp"
        android:text="TextView padding and margin"
        android:textSize="20sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#c5e1b0"
        android:textColor="#000000"
        android:text="TextView no padding no margin"
        android:textSize="20sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#f6c0c0"
        android:textColor="#000000"
        android:text="TextView no padding no margin"
        android:textSize="20sp" />

</LinearLayout>

相关的

Android相关问答推荐

时区日光节约改变问题

无法列出目录中的文件'

如何在"不同活动"中添加不同按钮?

如何让用户与我的应用生成的多个音频文件交互

如何在Jetpack Compose中使用Box Composable来实现这种布局?

为什么当我在装有Firebase的安卓系统中登录苹果时,收到的邮箱为空?

Android在NavHost中的LazyColumn中编写约束布局:error - replace()在未放置的项目上调用

空数据来自改装

看不到选项菜单栏

弹出导航堆栈后,Compose 无法访问 Hilt View Model

FFmpeg Android 错误

列语义在列中的第一个元素之后读取

修复错误 Invariant Violation: requireNativeComponent: "RNSScreenStackHeaderConfig" was not found in the UIManager

在 Jetpack Compose 中使用 ViewModel 实现 startActivity 的最佳实践

如何在 Android Jetpack compose 中为列表初始填充设置动画

Jetpack Compose TextField 在输入新字符时不更新

Jetpack Compose:mutableStateOf 不随流量更新

我应该使用 Bluetooth Classic 还是 Bluetooth LE 与我的移动应用程序通信?

如何从 Jetpack Compose 中的 Radio 组中获取价值

react-native-config 在发布版本中不起作用