Android自定义视图组委托addView [英] Android custom view group delegate addView
问题描述
在我想从FrameLayout
派生的情况下,我想实现自定义ViewGroup
,但是我希望从xml添加的所有子视图不直接添加到此视图中,而是直接添加到此自定义ViewGroup
中包含的FrameLayout
中.
让我显示示例以使其清楚.
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>
我想重定向,将所有子视图添加到ID为frame_layout_child_container
的FrameLayout
中.
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
-不会与addView(View child)
一起添加到其父级ViewGroup
中,这就是为什么仅覆盖该方法对您不起作用.您想覆盖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屋!