gpt4 book ai didi

android - 无法将 View 添加到以编程方式添加的 ViewGroups

转载 作者:搜寻专家 更新时间:2023-11-01 09:43:45 25 4
gpt4 key购买 nike

我无法将 subview 添加到以编程方式添加的 ViewGroup。

首先,我创建一个 GroupView (_view) 并将其添加到背景 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) 添加一个 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.

但是它没有被添加,或者它不可见等等。现在,如果我像这样使用 Background RelativeLayout (_root) _root.addView(dragIcon); 它工作正常,添加了 ImageView。

那么为什么不能使用 _view ViewGroup,_view.addView(dragIcon)

最佳答案

由于您正在使用 ViewGroup,这是一个抽象类,您应该实现抽象方法 onLayout,其中每个子项的每个 layout 方法必须调用 View 组。有关更多信息,请查看 this回答。

我认为您可以使用 FrameLayout,它是 ViewGroup 的子类。它实现了 onLayout 方法,因此您无需再次实现它。

这是 ViewGroup 的工作代码,我在其中明确调用了唯一一个子项 (dragIcon) 的 layout 方法。

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>

关于android - 无法将 View 添加到以编程方式添加的 ViewGroups,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38730182/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com