android - 如何忽略子元素的边距?
问题描述
背景
我正在通过将操作系统生成的通知视图注入我自己的布局来开发自定义通知布局。我的布局必须尽可能短,这是50dp
在 Android 10 中。
问题
我注入到视图中的视图有边距,导致它把我的布局50dp
从66dp
.
代码
以下布局简化了演示问题的过程:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
android:id="@+id/activity_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="50dp"
android:orientation="horizontal"
>
<TextView
android:id="@+id/full_height_view"
android:layout_width="10dp"
android:layout_height="match_parent"
android:background="@android:color/holo_blue_bright"
/>
<TextView
android:id="@+id/bad_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="46dp"
android:layout_marginBottom="20dp"
android:background="@android:color/holo_red_light"
>
</TextView>
</LinearLayout>
</FrameLayout>
请注意,我不想超过 50dp container
。minHeight
问题是从bad_view
总和到 66dp 的边距并将父级拉伸到 66dp。
问题
如何防止上边距bad_view
将父级拉伸超出其最小高度?我无法在父级上设置固定高度,因为确切的高度取决于操作系统。而且我无法修改bad_view
,因为它是由操作系统生成的。
解决方案
我找到了另一个解决方案:使用GridLayout
垂直权重来覆盖有问题的视图的高度。这给了我加权水平的灵活性,LinearLayout
但增加了垂直重量。
推荐阅读
- javascript - 导入的网格没有碰撞 - Babylon.js
- algorithm - 如何以每位球员的最大休息时间对循环赛进行排序?
- reactjs - 如何在 React 中使用 React-Map-GL 使 Geojson 图层可拖动?
- image - 将多个图像转换为 JPG 时出错
- laravel - 在 laravel 集合中使用 where 后缺少分页信息
- sql - SQL创建一个返回我所在步骤的函数
- reactjs - 会话没有快速设置
- mysql - 如何从最后 500 条记录中获取在线用户的总和?
- javascript - 使用jquery通过id获取单选按钮的值
- azure - 即使 Maven 任务失败,Azure devops 管道作业也显示成功