【问题标题】:Set the margins of an imageView by programming inside a constraintLayout通过在 constraintLayout 中编程来设置 imageView 的边距
【发布时间】:2020-11-01 10:54:42
【问题描述】:

我正在尝试使用此代码对 ConsraniLayout2 内的 ImageView 的边距进行编程

ConstraintLayout.LayoutParams newLayoutParams = (ConstraintLayout.LayoutParams) 
            imageView.getLayoutParams();
            newLayoutParams.topMargin = 140;
            newLayoutParams.leftMargin = 400;
            newLayoutParams.rightMargin = 0;
            newLayoutParams.bottomMargin = 0;

            imageView.setLayoutParams(newLayoutParams); 

使用此代码,我对 imageView 的站点进行编程,但对智能手机的整个屏幕进行编程。 如何使用 constrainLayout2 引用对 imageView 进行编程以切换位置?

【问题讨论】:

    标签: java android android-studio android-layout android-fragments


    【解决方案1】:

    您也可以尝试使用 ConstraintSet 设置边距。 下面是一个sn-p你可以试试

    val constraintSet = ConstraintSet()
    constraintSet.clone(constraintLayoutId)
    val marginTop = context.getDimension(10f)
    constraintSet.setMargin(R.id. imageView, ConstraintSet.TOP, marginTop)
    constraintSet.applyTo(constraintLayoutId)
    
    fun Context.getDimension(value: Float): Int {
        return TypedValue.applyDimension(
        TypedValue.COMPLEX_UNIT_DIP, value, this.resources.displayMetrics).toInt()
    }
    

    【讨论】:

    • 非常感谢,但是我是编程新手,不懂klotin怎么用,我只用java。
    猜你喜欢
    • 2019-02-08
    • 1970-01-01
    • 2020-08-18
    • 2018-03-17
    • 2020-08-27
    • 1970-01-01
    • 1970-01-01
    • 2011-01-29
    • 2019-10-19
    相关资源
    最近更新 更多