Android 自定义视图组委托 addView [英] Android custom view group delegate addView
问题描述
我想在我的情况下实现自定义 ViewGroup
派生自 FrameLayout
但我希望所有从 xml 添加的子视图不是直接添加到这个视图中,而是在 FrameLayout
包含在此自定义 ViewGroup
中.让我举个例子来说明清楚.
I want to implement custom ViewGroup
in my case derived from FrameLayout
but I want all child views added from xml to be added not directly into this view but in FrameLayout
contained in this custom ViewGroup
.
Let me show example to make it clear.
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="@+id/frame_layout_child_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<FrameLayout
android:id="@+id/frame_layout_top"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</merge>
而且我想将所有子视图重定向到 FrameLayout
,id 为 frame_layout_child_container
.
And I want to redirect adding all child view to FrameLayout
with id frame_layout_child_container
.
所以我当然会像这样覆盖方法 addView()
So of course I overrode methods addView()
like this
@Override
public void addView(View child) {
this.mFrameLayoutChildViewsContainer.addView(child);
}
但肯定这不起作用,因为这次 mFrameLayoutChildViewsContainer
没有添加到根自定义视图中.
But for sure this doesn't work because for this time mFrameLayoutChildViewsContainer
is not added to the root custom view.
我的想法是始终在此容器的顶部保持一些视图 frame_layout_top
并且添加到自定义组件中的所有子视图都应转到 frame_layout_child_container
My idea is always keep some view on on the top in this container frame_layout_top
and all child views added into custom component should go to frame_layout_child_container
使用自定义视图的示例
<CustomFrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"/>
</CustomFrameLayout>
所以在这种情况下 TextView
应该添加到 frame_layout_child_container
So in this case TextView
should be added to the frame_layout_child_container
是否可以像我描述的那样委托将所有视图添加到子 ViewGroup
中.
Is it possible to delegate adding all views into child ViewGroup
like I described.
我有其他想法,例如每次添加视图时使用 bringToFront()
方法以保持它们在正确的 z 轴顺序,或者例如在添加视图时,将其保存到数组而不是在膨胀后自定义视图将所有视图添加到该子视图 FrameLayout
I have other ideas like using bringToFront()
method every time view is added to keep them in correct z-axis order or for example when view is added, save it to array and than after inflating custom view add all views to this child FrameLayout
建议在这种情况下该怎么做,以免在每次添加新视图时重新填充所有布局而影响性能,如果可以以其他方式实现.
Suggest what to do in this case in order not to hit performance with reinflating all layout every time new view is added, if it is possible to implement in other way.
推荐答案
View
从布局膨胀 - 就像您的示例 TextView
- 没有添加到它们的父级 ViewGroup
和 addView(View child)
,这就是为什么仅覆盖该方法对您不起作用的原因.您想覆盖 addView(View child, int index, ViewGroup.LayoutParams params)
,所有其他 addView()
重载最终都会调用.
View
s inflated from a layout - like your example TextView
- are not added to their parent ViewGroup
with addView(View child)
, which is why overriding just that method didn't work for you. You want to override addView(View child, int index, ViewGroup.LayoutParams params)
, which all of the other addView()
overloads end up calling.
在该方法中,检查添加的孩子是否是您的两个特殊 FrameLayout
之一.如果是,让 super
类处理添加.否则,将子项添加到您的容器 FrameLayout
.
In that method, check if the child being added is one of your two special FrameLayout
s. If it is, let the super
class handle the add. Otherwise, add the child to your container FrameLayout
.
public class CustomFrameLayout extends FrameLayout {
private final FrameLayout topLayout;
private final FrameLayout containerLayout;
...
public CustomFrameLayout(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.custom, this, true);
topLayout = (FrameLayout) findViewById(R.id.frame_layout_top);
containerLayout = (FrameLayout) findViewById(R.id.frame_layout_child_container);
}
@Override
public void addView(View child, int index, ViewGroup.LayoutParams params) {
final int id = child.getId();
if (id == R.id.frame_layout_top || id == R.id.frame_layout_child_container) {
super.addView(child, index, params);
}
else {
containerLayout.addView(child, index, params);
}
}
}
这篇关于Android 自定义视图组委托 addView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!