首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >安卓:如何以编程方式设置layout_constraintRight_toRightOf "parent“

安卓:如何以编程方式设置layout_constraintRight_toRightOf "parent“
EN

Stack Overflow用户
提问于 2017-01-16 14:29:27
回答 4查看 57.8K关注 0票数 58

我在ConstrainLayout中的视图如下所示。

代码语言:javascript
复制
<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxWidth="260dp"
        android:textColor="#FFF"
        android:textSize="16sp"
        app:layout_constraintLeft_toLeftOf="@+id/parent"
        app:layout_constraintTop_toBottomOf="@id/message_date"
        android:id="@+id/text_main"
        />

基于某些条件,我想在recycleViewHolder中以编程方式将视图更改为app:layout_constraintLeft_toLeftOf="@+id/parent"layout_constraintLeft_toRightOf="@+id/parent"

EN

回答 4

Stack Overflow用户

发布于 2017-01-16 16:55:18

下面是一个使用java代码在父视图底部设置按钮的示例:

代码语言:javascript
复制
ConstraintLayout constraintLayout;
ConstraintSet constraintSet;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    constraintLayout = (ConstraintLayout) findViewById(R.id.activity_main_constraint_layout);

    Button button = new Button(this);
    button.setText("Hello");
    constraintLayout.addView(button);

    constraintSet = new ConstraintSet();
    constraintSet.clone(constraintLayout);

    constraintSet.connect(button.getId(), ConstraintSet.LEFT, constraintLayout.getId(), ConstraintSet.RIGHT, 0);
    constraintSet.constrainDefaultHeight(button.getId(), 200);
    constraintSet.applyTo(constraintLayout);

}

为了达到这样的效果,

代码语言:javascript
复制
app:layout_constraintLeft_toLeftOf="@+id/parent" 

您的java代码应该如下所示

代码语言:javascript
复制
set.connect(YOURVIEW.getId(),ConstraintSet.LEFT,ConstraintSet.PARENT_ID,ConstraintSet.LEFT,0);

为了达到这样的效果,

代码语言:javascript
复制
layout_constraintLeft_toRightOf="@+id/parent"

你的java代码应该是这样的,

代码语言:javascript
复制
set.connect(YOURVIEW.getId(),ConstraintSet.LEFT,ConstraintSet.PARENT_ID,ConstraintSet.RIGHT,0);

这里,我假设android:id="@+id/parent"是父ConstraintLayout的id。

票数 92
EN

Stack Overflow用户

发布于 2020-04-25 18:15:23

constraintSet =新ConstraintSet();以此类推

不管怎么说对我来说都不管用。

解决方式

代码语言:javascript
复制
  LayoutParams layoutParams = (LayoutParams) viewToChange.getLayoutParams();
    layoutParams.leftToLeft = anotherViewId;
    layoutParams.rightToRight =anotherViewId;
    layoutParams.topToTop = anotherViewId;
    layoutParams.bottomToBottom = anotherViewId;
    layoutParams.startToStart =anotherViewId;
    layoutParams.endToEnd = anotherViewId;
    viewToChange.setLayoutParams(layoutParams);
票数 16
EN

Stack Overflow用户

发布于 2018-11-27 21:37:16

使用id

代码语言:javascript
复制
class MyLayout(context: Context) : ConstraintLayout(context) {

    fun show() {
        val view = ImageView(context)
        addView(view)
        val params = view.layoutParams as ConstraintLayout.LayoutParams
        params.height = 100 
        params.width = 50
        params.rightToRight = id
        view.requestLayout()
    }
}
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41670618

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档