我正在尝试创建一个扩展RelativeLayout
的自定义 View 。
避免通过编码添加 View 。我用我的自定义相对布局准备了一个 xml。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android">
...
</RelativeLayout>
因此,在我的自定义 View 类中,我将通过
来扩充 xmlView.inflate(context, R.layout.custom_view, this);
我的问题是,由于我的类已经是 RelativeLayout
,如果我这样做,我将拥有两个级别的 RelativeLayout
。当然,我可以通过去掉xml中外层的RelativeLayout
来解决。但如果我这样做,我无法在 Eclipse 的 xml 编辑器中看到预览,这就是我希望自定义 View 从 xml 膨胀的原因。
即使我的自定义 View 类扩展了FrameLayout
, View 层次结构中也会多一层。如何解决这个问题?
最佳答案
为了减少不必要的额外层,需要使用合并标签。这是有关如何使用它的一个很好的示例。
http://android-developers.blogspot.com/2009/03/android-layout-tricks-3-optimize-by.html
<merge xmlns:android="http://schemas.android.com/apk/res/android">
...
</merge>
关于android - 通过膨胀 xml 自定义 ViewGroup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18971081/