2010-09-29 146 views
24

我試圖動態更改線性佈局或任何其他窗口小部件寬度或高度,但引發異常。動態更改Android上的線性佈局寬度或高度

我的佈局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/abc" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="ghghjkgj ghjg hjgj ghj g hjgjgh jhg " 
    /> 
</LinearLayout> 

和我的活動是:

public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     LinearLayout ll = (LinearLayout)findViewById(R.id.abc); 
     ll.setLayoutParams(new LinearLayout.LayoutParams(30,60)); 
    } 

投擲以下異常:

E/AndroidRuntime(16052): java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams 
E/AndroidRuntime(16052): at android.widget.FrameLayout.onLayout(FrameLayout.java:288) 
E/AndroidRuntime(16052): at android.view.View.layout(View.java:7035) 
E/AndroidRuntime(16052): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249) 
E/AndroidRuntime(16052): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125) 
E/AndroidRuntime(16052): at android.widget.LinearLayout.onLayout(LinearLayout.java:1042) 
E/AndroidRuntime(16052): at android.view.View.layout(View.java:7035) 
E/AndroidRuntime(16052): at android.widget.FrameLayout.onLayout(FrameLayout.java:333) 
E/AndroidRuntime(16052): at android.view.View.layout(View.java:7035) 
E/AndroidRuntime(16052): at android.view.ViewRoot.performTraversals(ViewRoot.java:1045) 
E/AndroidRuntime(16052): at android.view.ViewRoot.handleMessage(ViewRoot.java:1727) 
E/AndroidRuntime(16052): at android.os.Handler.dispatchMessage(Handler.java:99) 
E/AndroidRuntime(16052): at android.os.Looper.loop(Looper.java:123) 
E/AndroidRuntime(16052): at android.app.ActivityThread.main(ActivityThread.java:4627) 
E/AndroidRuntime(16052): at java.lang.reflect.Method.invokeNative(Native Method) 
E/AndroidRuntime(16052): at java.lang.reflect.Method.invoke(Method.java:521) 
E/AndroidRuntime(16052): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
E/AndroidRuntime(16052): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
E/AndroidRuntime(16052): at dalvik.system.NativeStart.main(Native Method) 

所以,我怎麼能更改尺寸動態?

回答

29

這裏的問題是,當你設置佈局參數的類型需要是父類,在這種情況下,這是一個FrameLayout。你需要這樣做:

ll.setLayoutParams(new FrameLayout.LayoutParams(30,60)); 
+0

非常感謝.......它對我非常有幫助,但我如何識別linearlayout的父項。 – Pratique 2010-10-04 11:03:28

48

入住這一點 - http://smartandroidians.blogspot.com/2010/04/changing-layout-height-dynamically.html

ll.getLayoutParams().height = 300; 
ll.getLayoutParams().width = 200; 
ll.requestLayout(); 

這個工程還對事件處理程序。

+5

它的作品,但應該記住的東西,該視圖應該有一個父母,否則getLayoutParams()將返回null – Shaegorath 2014-03-19 17:47:19

+0

謝謝多數民衆贊成 – Tom 2014-03-31 16:36:40

+0

在dp或px?... – 2015-10-05 22:42:05

4

這種簡單的方式做你的任務:如果你想給屏幕尺寸在高度和寬度,然後用下面的代碼

setContentView(R.id.main);  
LinearLayout layout= (LinearLayout) findViewById(R.id.left); 
int width=60; 
int height=60; 
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(width,height); 
layout.setLayoutParams(parms); 

和另一種方式:

setContentView(R.id.main);  
Display display = getWindowManager().getDefaultDisplay(); 
LinearLayout layout= (LinearLayout) findViewById(R.id.left); 
int width=display.getWidth(); 
int height=display.getHeight(); 
LinearLayout.LayoutParams parms = new inearLayout.LayoutParams(width,height); 
layout.setLayoutParams(parms); 

希望使用全給你。