如何在CardView中正确设置ImageView

问题描述 投票:0回答:1

因此,我正在制作类似应用程序的图库,并且其中需要一些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上实现类似的效果

android kotlin android-cardview
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.