【问题标题】:Can't add views to ViewGroups added programmatically无法将视图添加到以编程方式添加的 ViewGroups
【发布时间】:2016-08-04 08:36:32
【问题描述】:

我无法将子视图添加到以编程方式添加的 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.

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

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

【问题讨论】:

  • 避免使用 _VarName 作为实例变量,即使是 android studio code inspect 也会建议你对非公共和非静态字段使用 m 前缀。
  • 确保将 dragIcon 添加到 _view,然后将 _view 添加到 _root。

标签: android android-viewgroup


【解决方案1】:

由于您使用的是ViewGroup,这是一个抽象类,您应该实现抽象方法onLayout,其中必须调用视图组每个子视图的每个layout 方法。欲了解更多信息,请查看this 答案。

我认为您可以使用 ViewGroup 的子类 FrameLayout。它实现了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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-09
    • 2017-06-28
    • 1970-01-01
    • 2020-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多