include和merge的问题

本文探讨了在Android XML布局文件中使用merge标签时遇到的Padding失效问题,并通过调整布局结构找到了解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

include和merge的问题


发现一个问题

merge文件

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>

<ImageView
android:id="@+id/theme_toolbar_activity_img_sigle_selected"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/theme_toolbar_activity_selected"
android:visibility="gone" />

<TextView
android:id="@+id/theme_toolbar_activity_txt_sigle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/theme_layout_single_toolbar"
android:textColor="@color/theme_toolbar_activity_txt_default"
android:textSize="@dimen/dp_12" />

</merge>


然后 我引用

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/theme_layout_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:paddingLeft="@dimen/dp_20"
android:paddingRight="@dimen/dp_20"
android:paddingBottom="@dimen/dp_20"
android:paddingTop="@dimen/dp_10"
>
<include
layout="@layout/theme_toolbar_activity_include"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
/>
</RelativeLayout>



[color=red]这个时候 padding就不起作用了 即使我在代码手动设置padding也是不起作用的 并且 即使我把merge给位RelativeLayout,依然是这样 不过这个时候 我在include上面 设置margin就好使了 但是 还是有一个多余的节点。[/color]

但是 我在另一个xml里面 引入merge就可以,对比了一下 唯一的区别 就是 这个父view是根节点,所以 我把上面的修改了一下

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/theme_layout_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:fitsSystemWindows="true"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/dp_20"
android:paddingRight="@dimen/dp_20"
android:paddingBottom="@dimen/dp_20"
android:paddingTop="@dimen/dp_10"
>
<include
layout="@layout/theme_toolbar_activity_include"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
/>
</RelativeLayout>
</RelativeLayout>



呃...好了  ̄□ ̄||
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值