android – 没有应用于我的TextView的约束

android – 没有应用于我的TextView的约束,第1张

概述这里我有一些代码,它以编程方式向ConstraintLayout添加一些元素 public void AddCategory(final String label, String img){ final float scale = getResources().getDisplayMetrics().density; //Create a new constrai 这里我有一些代码,它以编程方式向ConstraintLayout添加一些元素

public voID Addcategory(final String label,String img){        final float scale = getResources().getdisplayMetrics().density;        //Create a new constraint layout        ConstraintLayout constraintLayout = new ConstraintLayout(getContext());        servicesLayout.addVIEw(constraintLayout);        constraintLayout.setID(VIEw.generateVIEwID());        androID.support.v7.Widget.GrIDLayout.LayoutParams layoutParams = new androID.support.v7.Widget.GrIDLayout.LayoutParams(androID.support.v7.Widget.GrIDLayout.spec(androID.support.v7.Widget.GrIDLayout.UNdefineD,1f),androID.support.v7.Widget.GrIDLayout.spec(androID.support.v7.Widget.GrIDLayout.UNdefineD,1f));        layoutParams.wIDth = 0;        layoutParams.height = (int) scale * 200;        layoutParams.setmargins((int) scale * 15,(int) scale * 15,(int) scale * 15);        constraintLayout.setLayoutParams(layoutParams);        constraintLayout.setBackgroundcolor(getResources().getcolor(R.color.colorPrimary));        //Place ImageVIEw in layout        ImageVIEw imageVIEw = new ImageVIEw(getContext());        constraintLayout.addVIEw(imageVIEw);        imageVIEw.setID(VIEw.generateVIEwID());        ConstraintSet constraintSet = new ConstraintSet();        constraintSet.connect(imageVIEw.getID(),ConstraintSet.top,constraintLayout.getID(),ConstraintSet.top);        constraintSet.connect(imageVIEw.getID(),ConstraintSet.BottOM,ConstraintSet.BottOM);        constraintSet.connect(imageVIEw.getID(),ConstraintSet.left,ConstraintSet.left);        constraintSet.connect(imageVIEw.getID(),ConstraintSet.RIGHT,ConstraintSet.RIGHT);        constraintSet.applyTo(constraintLayout);        if (getContext() != null){            GlIDeApp.with(this)                    .load(img)                    .centerCrop()                    .Transition(DrawableTransitionOptions.withCrossFade())                    .placeholder(R.color.colorGray)                    .error(R.drawable.Title)                    .into(imageVIEw);        }        //Place label background        ImageVIEw captionImageVIEw = new ImageVIEw(getContext());        constraintLayout.addVIEw(captionImageVIEw);        captionImageVIEw.setID(VIEw.generateVIEwID());        ConstraintSet captionConstraintSet = new ConstraintSet();        captionConstraintSet.connect(captionImageVIEw.getID(),ConstraintSet.BottOM);        captionConstraintSet.connect(captionImageVIEw.getID(),ConstraintSet.left);        captionConstraintSet.connect(captionImageVIEw.getID(),ConstraintSet.RIGHT);        captionConstraintSet.applyTo(constraintLayout);        captionImageVIEw.getLayoutParams().wIDth = ConstraintLayout.LayoutParams.MATCH_CONSTRAINT;        captionImageVIEw.getLayoutParams().height  = (int) scale * 50;        captionImageVIEw.setBackgroundcolor(getResources().getcolor(R.color.colorPrimaryDark));        captionImageVIEw.setAlpha(0.7f);        //Place text in front of the label backgroudn        TextVIEw textVIEw = new TextVIEw(getContext());        constraintLayout.addVIEw(textVIEw);        textVIEw.setID(VIEw.generateVIEwID());        ConstraintSet labelConstraintSet = new ConstraintSet();        labelConstraintSet.connect(textVIEw.getID(),captionImageVIEw.getID(),ConstraintSet.BottOM);        labelConstraintSet.connect(textVIEw.getID(),ConstraintSet.left);        labelConstraintSet.connect(textVIEw.getID(),ConstraintSet.RIGHT);        labelConstraintSet.applyTo(constraintLayout);        textVIEw.getLayoutParams().wIDth = ConstraintLayout.LayoutParams.WRAP_CONTENT;        textVIEw.getLayoutParams().height  = ConstraintLayout.LayoutParams.WRAP_CONTENT;        textVIEw.setTextcolor(getResources().getcolor(R.color.colorWhite));        textVIEw.setText(label);}

前3个代码块工作得很好:以编程方式添加ConstraintLayout和两个ImageVIEw.问题在于尝试约束TextVIEw元素.它只是在尝试将其约束到其中一个需要的ImageVIEw时无法正常工作.将它约束到ConstraintLayout时它会起作用,但这是不可取的.相反,它将忽略所有constraintSet.connect代码并将其放置在布局的默认右上角位置,如图所示

解决方法 更改TextVIEw的constraintSet的endID,如下所示: –

ConstraintSet labelConstraintSet = new ConstraintSet();    labelConstraintSet.connect(textVIEw.getID(),(int)scale*15);    labelConstraintSet.connect(textVIEw.getID(),ConstraintSet.left);    labelConstraintSet.connect(textVIEw.getID(),ConstraintSet.RIGHT);
总结

以上是内存溢出为你收集整理的android – 没有应用于我的TextView的约束全部内容,希望文章能够帮你解决android – 没有应用于我的TextView的约束所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1126204.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存