繁体   English   中英

以编程方式为 kotlin 中 ImageView 的 layout_weight 编码

[英]programmatically code for layout_weight for ImageView in kotlin

你好,我想用kotlin代码以编程方式为 imageview 设置Layout_weight以确保 layout_weight 不是 weightsum 就像下面的代码

    fun stack(x:Int){
        
        
        LinearLayout1.weightSum= 1F
        imageView1.visibility=View.VISIBLE
        imageView2.visibility=View.INVISIBLE
        imageView3.visibility=View.INVISIBLE
        if (x==1) {
            imageView1.visibility=View.VISIBLE
            imageView1.layout_weight=1F  ***//I want this***
            imageView2.visibility=View.INVISIBLE
            imageView3.visibility=View.INVISIBLE
        }
        else if (x==2){
            imageView1.visibility=View.INVISIBLE
            imageView2.visibility=View.VISIBLE
            imageView2.layout_weight=1F    ***//I want this***
            imageView3.visibility=View.INVISIBLE
        }
        else if (x==3){
            imageView1.visibility=View.INVISIBLE
            imageView2.visibility=View.INVISIBLE
            imageView3.visibility=View.VISIBLE
            imageView3.layout_weight=1F  ***//I want this***

        }
        
    }

用这个:

var params = LinearLayout.LayoutParams(
    LayoutParams.MATCH_PARENT,
    LayoutParams.MATCH_PARENT,
    1.0f
)
imageView1.setLayoutParams(params)

在这一行中: var param = LinearLayout.LayoutParams(我们使用LinearLayout因为您的图像视图在LinearLayout

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM