2014-05-08 73 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