4

J'ai ma CardView dans ma mise en page XML et j'ai besoin de lui attribuer des marges par programme (car je n'ai pas besoin de marge à tout moment. marge).Définition de la marge par programme à CardView

Voici comment je l'ai fait:

CardView.LayoutParams layoutParams = (CardView.LayoutParams) myCardView.getLayoutParams(); 
layoutParams.setMargins(0, 0, SystemHelper.dpToPx(2), 0); 
myCardView.setLayoutParams(layoutParams); 

Cela a bien fonctionné. Il a mis 2dp marge au bas de mon CardView. Cependant, je reçois dans mon logcat:

java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams 

Je changé CardView.LayoutParams à FrameLayout.LayoutParams, comme ceci:

FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) myCardView.getLayoutParams(); 
layoutParams.setMargins(0, 0, SystemHelper.dpToPx(2), 0); 
myCardView.setLayoutParams(layoutParams); 

Et encore une fois, cela fonctionne, mais je reçois la même erreur que ci-dessus. J'ai donc modifié une fois de plus pour utiliser LinearLayout.LayoutParams.

LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) myCardView.getLayoutParams(); 
layoutParams.setMargins(0, 0, SystemHelper.dpToPx(2), 0); 
myCardView.setLayoutParams(layoutParams); 

Quand je fais cela, je ne suis pas l'erreur mais il ne fixe aucune marge.

Dois-je simplement ignorer l'erreur? Cela ne semble pas juste.

EDIT:

Voici mon CardView en XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:orientation="horizontal" 
    android:background="@android:color/white" 
    android:minHeight="@dimen/item_min_height" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <android.support.v7.widget.CardView 
     android:id="@+id/cardViewAppointmentWeek" 
     app:cardElevation="2dp" 
     android:layout_marginBottom="2dp" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <LinearLayout 
      android:orientation="horizontal" 
      android:background="?android:attr/selectableItemBackground" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

      ... 

     </LinearLayout> 

    </android.support.v7.widget.CardView> 

</LinearLayout> 
+0

Pourriez-vous me montrer le dans votre code Vignettes xml? – redAllocator

+0

@red_allocator J'ai mis à jour ma question pour montrer le 'CardView' en xml –

+0

Qui est le parent direct de votre' CardView'? – azizbekian

Répondre

8

Dans votre cas, vous êtes pas particulièrement intéressé qui est le parent de votre CardView, parce que la seule chose que vous voulez changer est la marge. Toutes les classes sont *LayoutParams enfants directs/indirects de MarginLayoutParams, ce qui signifie que vous pouvez facilement jeter à MarginLayoutParams et effectuer des changements sur cet objet:

 


    ViewGroup.MarginLayoutParams layoutParams = 
          (ViewGroup.MarginLayoutParams) myCardView.getLayoutParams(); 
    layoutParams.setMargins(0, 0, SystemHelper.dpToPx(2), 0); 
    myCardView.requestLayout(); 

 
+0

il m'a aidé. Je vous remercie – MinMaxUz

0
  1. Set LinearlayoutId votre enfant
  2. Vignettes Recherche LinearLayout de findViewById.
  3. Set LayoutParams

    LinearLayout LinearLayout = (LinearLayout) myCardView.findViewById (R.id.linearlayoutid); LinearLayout.LayoutParams layoutParams = nouveau LinearLayout.LayoutParams ( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); layoutParams.setMargins (30, 20, 30, 0);

+0

Je ne veux pas mettre de padding. Je veux définir une marge. C'est complètement différent –

+0

Vous voulez dire que LinearLayout doit se déplacer pour margine? – redAllocator

+0

Je ne veux rien faire avec mon 'LinearLayout'. S'il vous plaît lire ma question originale. Je veux définir la marge '2dp' sur mon' CardView' par programmation. –