如何在CardView中正确设置ImageView

所以我正在制作类似app的图库,并且其中需要一些ScrollView和CardView。

This is the screenshot when I put some background in the ImageView

This is how it suppose to look,I have removed the background image here

我尝试将ImageView放入具有线性布局的CardView内,但是图像填充了ScrollView。

fun CreateCardView(ItemCount: Int){                                                         
   for (i in 1 until ItemCount){                                                           

       val card_view = CardView(this)                                                      
       val param = GridLayout.LayoutParams()                                               
       param.rowSpec = GridLayout.spec(GridLayout.UNDEFINED,1f)                           
       param.columnSpec = GridLayout.spec(GridLayout.UNDEFINED,1f)                        
       card_view.layoutParams = param                                                      
       val param2 = card_view.layoutParams as GridLayout.LayoutParams                                                                            
       card_view.layoutParams = param2                                                     
       subLayoutGrid?.addView(card_view)                                                                      

       val linearLayout = LinearLayout(this)                                               
       val layoutParams = WindowManager.LayoutParams(                                      
           WindowManager.LayoutParams.WRAP_CONTENT,// CardView width                      
           WindowManager.LayoutParams.WRAP_CONTENT // CardView height                      
       )                                                                                                                                       
       linearLayout.layoutParams = layoutParams                                                                               
       linearLayout.setBackgroundColor(Color.parseColor("#135517"))                        
       card_view.addView(linearLayout)                                                     


       val imageView = ImageView(this)                                                     
       val imageParams = WindowManager.LayoutParams(                                       
           WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.MATCH_PARENT                                         
       )                                                                                   
       imageView.layoutParams = imageParams                                                                                                       
       imageView.setBackgroundResource(R.drawable.animaleight)                             
       val layout = findViewById<LinearLayout>(finallinearId)                              
       layout.addView(imageView)                                                           


   }                                                                                       

}

如何使用ImageView在图像2上实现类似的效果

loveniba 回答:如何在CardView中正确设置ImageView

我相信这是因为您将cardView的宽度和高度设置为WRAP_CONTENT,并且您在此处放置的图像很大,因此您应该更改图像的大小或使用固定的cardView大小,例如100dp高度和100dp-宽度,但这不是最佳选择,否则您可以尝试更改xml文件,如果您将xml文件放入,我会为您提供帮助。 另外,只要是LinearLayout,就尝试将每个项目的“重量”参数设置为“ 1”

本文链接:https://www.f2er.com/3162139.html

大家都在问