简体   繁体   中英

Dynamically changing the linearlayout width or height on Android

I am trying to change linear layout or any other widget width or height dynamically but throwing exception.

My layout is:

<?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>

and My activity is:

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));
    }

Throwing following exception:

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)

So how can I change the dimensions dynamically?

Check this - http://smartandroidians.blogspot.com/2010/04/changing-layout-height-dynamically.html

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

This works also on event handler.

The problem here is that when you set the layout params the type needs to be that of the parent, in this case that is a FrameLayout. You need to do this:

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

This simple way to do your task:

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);

and another way if you want to give screen size in height and width then use below code :

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);

hope use full to you.

You can use this method inside your utils class to set height and width dynamically.

 public void setWidthAndHeight(Context context,RelativeLayout view,int width, int height) {
                width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, width, context.getResources().getDisplayMetrics());//used to convert you width integer value same as dp
                height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, height, context.getResources().getDisplayMetrics());
               //note : if your layout is LinearLayout then use LinearLayout.LayoutParam
                view.setLayoutParams(new RelativeLayout.LayoutParams(width, height));
         }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM