2014-05-08 71 views
0

我得到這個錯誤:java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams變化RelativeLayout的高度

我認爲錯誤的事實,我有一個線性佈局 一個相對佈局我找了同樣的問題造成的:Set RelativeLayout layout params programmatically - throws ClassCastException

但這沒「T幫助

我的代碼:

LinearLayout.LayoutParams paramsT1score = (LinearLayout.LayoutParams)mT1layoutScore.getLayoutParams(); 
       // Changes the height and width to the specified *pixels* 
       paramsT1score.height = 80; 
       mT1layout.setLayoutParams(paramsT1score); 

這是我的.xml

</RelativeLayout> 



<LinearLayout 
    android:id="@+id/linearLayout2" 
    android:layout_width="fill_parent" 
    android:layout_height="70dp" 
    android:orientation="horizontal" > 

    <RelativeLayout 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="70dp" 
     android:background="#314ebd" 
     android:id="@+id/LayoutT1score" 
     android:layout_toLeftOf="@+id/LayoutT2score"> 

     <TextView 
      android:id="@+id/player1" 
      android:layout_width="80dp" 
      android:layout_height="wrap_content" 
      android:ems="10" 
      android:inputType="number" 
      android:text="0" 
      android:textSize="24dp" 
      android:layout_alignParentBottom="true" 
      android:layout_centerHorizontal="true" 
      android:layout_marginBottom="10dp" 
      android:textColor="#ffffff" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Team 1" 
      android:id="@+id/lblTeam1" 
      android:layout_above="@+id/player1" 
      android:layout_alignLeft="@+id/player1" 
      android:layout_alignStart="@+id/player1" 
      android:textSize="24dp" 
      android:textColor="#ffffff" /> 

    </RelativeLayout> 

    <RelativeLayout 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="60dp" 
     android:id="@+id/LayoutT2score" 
     android:background="#fbff62" 
     > 

     <TextView 
      android:id="@+id/player2" 
      android:layout_width="80dp" 
      android:layout_height="wrap_content" 
      android:ems="10" 
      android:inputType="number" 
      android:text="0" 
      android:textSize="24dp" 
      android:layout_alignParentBottom="true" 
      android:layout_centerHorizontal="true" 
      android:layout_marginBottom="0dp" 
      android:textColor="#000000" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Team 2" 
      android:id="@+id/lblTeam2" 
      android:textSize="24dp" 
      android:textColor="#000000" 
      android:layout_above="@+id/player2" 
      android:layout_alignLeft="@+id/player2" 
      android:layout_alignStart="@+id/player2" /> 


    </RelativeLayout> 
</LinearLayout> 

回答

1

佈局PARAMS類型由父類型決定。您從一個視圖mT1layoutScore中獲取佈局參數,然後嘗試在另一個mT2layout中設置它們,其父類型是不同的。

在相同的對象上設置佈局參數,或者因爲您正在編輯佈局參數,只需調用mT1layoutScore.requestLayout()即可。

+0

你能給我一個廣泛的例子嗎? – matthijsW

+0

用'mT1layoutScore.requestLayout()'替換'mT2layout.setLayoutParams()'。 – laalto

+0

THanks,複製的典型例子出錯了。我想我需要更多的睡眠 – matthijsW

0

嗯,好像mT1layoutScore的params爲LinearLayout.LayoutParams,但mT2layout的params爲RelativeLayout.LayoutParams

所以,我認爲你應該創建

RelativeLayout.LayoutParams paramsT2score = (RelativeLayout.LayoutParams)mT2layout.getLayoutParams(); 

然後從paramsT1score複製所需的所有PARAMS到paramsT2score,然後加入

mT2layout.setLayoutParams(paramsT2score); 
+0

仍然收到相同的錯誤:android.widget.LinearLayout $ LayoutParams無法轉換爲android.widget.RelativeLayout $ LayoutParams – matthijsW

+0

在鏈接中提到您需要使用te父類型的佈局請參閱link:Set RelativeLayout layout params以編程方式 - 拋出ClassCastException – matthijsW

+0

@matthijsW嗯,現在我記得...我改變我的佈局後有同樣的錯誤。嘗試清理和重建項目。 – Suvitruf