4

我在我的XML佈局中有一個CardView,我需要以編程方式爲它設置邊距(這是因爲我不需要任何時間保證金,基於很少的條件,我可以設置或刪除保證金)。以編程方式設置邊距到CardView

我是這樣做的:

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

這工作得很好。它把2dp保證金在我CardView的底部。不過,我得到這個在我的logcat:

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

因此,我改變CardView.LayoutParamsFrameLayout.LayoutParams,像這樣:

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

而且再一次,它的工作原理,但我得到了同樣的錯誤如上。

所以我修改了它多了一個使用LinearLayout.LayoutParams時間。

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

當我這樣做,我不明白的錯誤但是它不設置任何保證金

如果我只是忽略這個錯誤嗎?這看起來不正確。

編輯:

這是我在XML CardView

<?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

你能告訴我你的XML代碼cardview? – redAllocator

+0

@red_allocator我已更新我的問題以顯示xml中的'CardView' –

+0

誰是你的'CardView'的直接父母? – azizbekian

回答

8

你的情況,你是不是專門感興趣誰是你CardView的父母,因爲你想改變的僅僅是保證金。所有*LayoutParams類是MarginLayoutParams直接/間接的孩子,這意味着你可以很容易地轉換爲MarginLayoutParams和對象上執行的變化:

 


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

 
+0

它對我有幫助。謝謝 – MinMaxUz

0
  1. 設置從findViewById LinearlayoutId您cardview孩子
  2. 查找LinearLayout中。
  3. 集的LayoutParams

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

+0

我不想設置填充。我想設定保證金。它是完全不同的 –

+0

你是說LinearLayout必須移動邊緣? – redAllocator

+0

我不想用我的'LinearLayout'做任何事情。請閱讀我原來的問題。我想以編程方式在我的'CardView'上設置'2dp'的邊距。 –

相關問題