我无法将子视图添加到我以编程方式添加的ViewGroup中。
首先,我创建一个GroupView(_view
)并将其添加到Background RelativeLayout(_root
),就像这样......
_view = new ViewGroup(MainActivity.this) {
@Override
protected void onLayout(boolean b, int i, int i1, int i2, int i3) {
}
};
_view.setBackgroundColor(0xFF00FF00);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 200);
params.setMargins(10, 0, 10, 0);
_view.setLayoutParams(params);
_view.setTranslationX(0);
_view.setTranslationY(positionY - 80);
selectView(_view);
_root.addView(_view);
_view.setTag(R.string.viewID, "" + viewNum);
_view.setTag(R.string.viewSelected, "" + 0);
viewNum ++;
工作正常。然后我尝试向这个新的GroupView(_view
)添加一个视图......
ImageView dragIcon = new ImageView(MainActivity.this);
dragIcon.setImageResource(R.drawable.drabicon);
RelativeLayout.LayoutParams imgParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
imgParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
imgParams.setMargins(0,0,30,0);
dragIcon.setLayoutParams(imgParams);
dragIcon.setTranslationY(_view.getTranslationY());
_view.addView(dragIcon); // This Is What I Want, But This Doesn't Work.
//_root.addView(dragIcon); // Don't Want This, But This Works.
但它没有被添加,或者它不可见或什么的。现在,如果我像这样使用背景RelativeLayout(_root
)_root.addView(dragIcon);
它工作得很好,ImageView就会被添加。
那么为什么不能使用_view
ViewGroup,_view.addView(dragIcon)
?
答案 0 :(得分:2)
由于您使用的是ViewGroup
,这是一个抽象类,您应该实现抽象方法onLayout
,其中必须调用视图组的每个子节点的每个layout
方法。有关详情,请查看this answer。
我认为你可以使用FrameLayout这是一个子类ViewGroup。它实现了onLayout
方法,因此您不必再次实施它。
这是ViewGroup的工作代码,我明确地称之为唯一一个子节点的layout
方法(dragIcon)。
<强> ExampleActivity.java 强>
public class ExampleActivity extends AppCompatActivity {
ViewGroup _view ;
RelativeLayout _root;
ImageView dragIcon;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_example);
_root = (RelativeLayout) findViewById(R.id.exampleLayout);
_view = new ViewGroup(ExampleActivity.this) {
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
dragIcon.layout(l, t, r, b); //CHECK THIS
}
};
_view .setBackgroundColor(0xFF00FF00);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 200);
params.setMargins(10, 0, 10, 0);
_view .setLayoutParams(params);
_view .setTranslationX(0);
_view .setTranslationY(80);
// selectView(_view);
_root.addView(_view );
_view .setTag(R.string.app_name, "" );
_view .setTag(R.string.app_name, "" + 0);
dragIcon = new ImageView(ExampleActivity.this);
dragIcon.setImageResource(R.drawable.heart1);
RelativeLayout.LayoutParams imgParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
imgParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
_view .addView(dragIcon); // This Is What I Want, But This Doesn't Work.
//sample_root.addView(dragIcon); // Don't Want This, But This Works.
}
}
<强> activity_example.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/exampleLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.anil.raceorganizer.ExampleActivity">
</RelativeLayout>