Set CardView height programmatically

前端 未结 3 1413
不思量自难忘°
不思量自难忘° 2021-02-19 20:48

I am new to Android dev and I\'m having trouble trying to set the min height of a it.gmariotti.cardslib.library.view.CardView programmatically. I create a new insta

3条回答
  •  伪装坚强ぢ
    2021-02-19 21:08

    CardView extends FrameLayout so you should be able to set LayoutParams. Try something like this :

     CardView.LayoutParams layoutParams = (CardView.LayoutParams)
                catCard.getLayoutParams();
        layoutParams.height = 10;
    

    , dont forget that setting Width is also required. Or create new LayoutParams like this (not tested) :

    CardView catCard = new CardView(getApplicationContext());
    // sets width to wrap content and height to 10 dp ->
    catCard.setLayoutParams(new CardView.LayoutParams(
         CardView.LayoutParams.WRAP_CONTENT, 10));
    
    catCard.setMinimumHeight(10);
    

提交回复
热议问题