You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何去除9 patch阴影模糊产生的额外边距,实现LinearLayout无内边距背景

问题原因

你遇到的额外内边距是9Patch图的内容区域标记规则导致的:9Patch图的四条边中,底部和右侧的黑色标记线用来定义内容可填充的区域,你用工具生成的带阴影的9Patch默认把阴影范围纳入了边距计算,所以内容会自动向内缩进,和阴影区域留出空隙。

解决方法

方法1:修改9Patch图(推荐,永久生效)

这是最根本的解决方式,修改后无需额外加布局配置:

  • 将你生成的pls.9.png文件拖入Android Studio,打开9Patch编辑界面
  • 调整底部和右侧的黑色标记线,将标记范围对齐到排除阴影后的实际内容矩形边界
  • 可勾选编辑器的「Show content」预览选项,确认内容填充区域符合预期后保存即可。

方法2:布局中强制覆盖内边距

如果不想修改原图,直接给设置了9Patch背景的LinearLayout手动指定padding为0,手动设置的padding会覆盖9Patch自带的默认边距,修改后的XML代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingHorizontal="26dp"
    android:paddingVertical="22dp"
    tools:context=".TeamDetailActivity"
    android:id="@+id/profile_container"
    android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/pls"
                android:padding="0dp"
                android:orientation="vertical">

                    <ImageView
                        android:layout_width="50dp"
                        android:layout_height="wrap_content"
                        android:adjustViewBounds="true"
                        android:scaleType="fitXY"
                        android:src="@drawable/psg" />

            </LinearLayout>

</LinearLayout>

内容的提问来源于stack exchange,提问作者Foxter

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 04:45:05