Reference:
http://www.cnblogs.com/wanqieddy/p/4040601.htmlCode snippet:
layout.xml:<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/my_relative_layout_id"
android:layout_width="match_parent"
android:layout_height="match_parent">
................
class.java:
RelativeLayout mRelativeLayout = (RelativeLayout) findViewById(R.id.my_relative_layout_id);
mRelativeLayout.setLayoutParams(new LinearLayout.LayoutParams(200,600));
Note:
setLayoutParams裡的參數型態不是要改變高度的 layout型態,而是要和外面一層layout的型態相同(如上面紅字所標註),否則會出現 ClassCastException以上面的範例來說,要改變高度的 layout型態是 RelativeLayout,如果 setLayoutParams裡面帶入的參數型態為 RelativeLayout.LayoutParams,就會出現下面的 exception
java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams
因為要改變高度的 layout,外面一層的 layout型態為 LinearLayout,所以setLayoutParams裡面帶入的參數型態要是 LinearLayout.LayoutParams
沒有留言:
張貼留言