我得到這個錯誤: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>
你能給我一個廣泛的例子嗎? – matthijsW
用'mT1layoutScore.requestLayout()'替換'mT2layout.setLayoutParams()'。 – laalto
THanks,複製的典型例子出錯了。我想我需要更多的睡眠 – matthijsW