【问题标题】:Added View programmatically to Constraint Layout jumps to origin以编程方式将视图添加到约束布局跳转到原点
【发布时间】:2018-07-21 16:37:36
【问题描述】:

我正在用 android 制作一些动画,并且我在足球场内有球员。对于一些我想添加标签的元素,添加一些约束以将它们与它们各自的视图分组,并且当动画开始时,标签会跟随它们。

我已经尝试了这两种解决方案,但是 textview 一直跳到约束布局的原点:

How to programmatically add views and constraints to a ConstraintLayout?

Add a view programmatically in ConstraintLayout

我已经为这些解决方案尝试了很多变体,但都没有成功。我当然错过了一些东西......任何想法将不胜感激!

我创建视图的代码:

   //init imageview
                ImageView imageView = new ImageView(this);
                imageView.setId(View.generateViewId());
                imageView.setTag(key);
                imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                //buscar el drawable correspondiente
                imageView.setImageDrawable(setViewDrawable(vista));
                imageView.setOnLongClickListener(this);
//set width and height of the view
                Pair<Integer, Integer> widthAndHeight = getWidthAndHeight(key);
                //set ancho y alto
                ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(widthAndHeight.first, widthAndHeight.second);
                imageView.setLayoutParams(params);
                mTacticLayout.addView(imageView);
                //set position
                if (!isEdit) {
                    Pair<Float, Float> xAndY = getViewXY(key);
                    Float rotation = getViewRotation(key);
                    //account for offsets
                    imageView.setX(xAndY.first - widthAndHeight.first / 2);
                    imageView.setY(xAndY.second - widthAndHeight.second / 2);
                    imageView.setRotation(rotation);
                }

//HERE HERE HERE LOOK AT ME  
       if(imageView.getTag().toString().equals(r.getString(R.string.ball_tag))){
                    TextView textView = new TextView(this);
                    textView.setId(View.generateViewId());
                    textView.setText("My View");

                    mTacticLayout.addView(textView);
                    ConstraintSet set = new ConstraintSet();
                    set.clone(mTacticLayout);
                    set.connect(textView.getId(), ConstraintSet.TOP, imageView.getId(), ConstraintSet.BOTTOM, 0);
                    set.connect(textView.getId(), ConstraintSet.RIGHT, imageView.getId(), ConstraintSet.RIGHT, 0);
                    set.connect(textView.getId(), ConstraintSet.LEFT, imageView.getId(), ConstraintSet.LEFT, 0);
                    set.applyTo(mTacticLayout);
                }

【问题讨论】:

  • 这有点困难,但在 XML 中定义一个组并通过代码查看该组视图

标签: java android programmatically android-constraintlayout


【解决方案1】:

setX() 根据documentation 执行以下操作:

设置此视图的视觉 x 位置,以像素为单位。这相当于将translationX属性设置为传入的x值与当前left属性的差值。

setTranslationX()也是按照documentation做的:

设置此视图相对于其左侧位置的水平位置。除了对象的布局放置它的位置之外,这有效地定位了布局后的对象。

setY()setTranslationY() 对 y 坐标执行相同的操作。

这里的关键词是post-layout。您的 ImageView 被放置在顶部/左侧,因为它没有约束。 TextView 根据其约束放置在ImageView 上,因此也放置在顶部/左侧。现在两个视图都位于顶部/左侧,ImageView 将根据 setX()setY() 移动,留下 TextView 后面。

有几种方法可以解决此问题。可能最简单的方法是将应用于ImageView 的相同翻译应用于TextView

【讨论】:

  • 我试试看!动画开始时标签会移动吗?你的猜测是什么?
  • @Koopa 不能说它是否会出现在动画中。
  • 因此,在将翻译应用到 TextView 之后,标签正确放置在 ImageView 下方。但是,在设置动画(涉及 ImageView 的拖放)之后,标签不会跟随 ImageView。标签的边距始终保持为零(也许是“希望”调整了边距,并且我可以以某种方式解决这个问题)。
  • @Koopa 这将是下一个问题。如果TextViewImageVIew 总是在一起,您可能需要考虑`compound control'
  • 嗯,没用过。我会看看它是否适合我的情况,特别是考虑到我的视图有拖放以及旋转交互。干杯。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多