Is it possible to change the layout width and height in Android at run time?
I have two relative layouts. Both have width of fill parent and height of 100 dip. My requirement is when I click the first layout it should shrink to 50dip height and the other expand to the 150dip height. While doing this I got exception. Please help me how to change the layout width and height at run time.
final LinearLayout RLChange = new LinearLayout(this);
RLChange.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
RLChange.setOrientation(LinearLayout.VERTICAL);
final LinearLayout RLGreen = new LinearLayout(this);
RLGreen.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
300));
RLGreen.setBackgroundColor(Color.GREEN);
RLGreen.setOrientation(LinearLayout.HORIZONTAL);
Button btnClick = new Button(this);
btnClick.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
btnClick.setBackgroundColor(Color.RED);
RLGreen.addView(btnClick);
final LinearLayout RLYellow = new LinearLayout(this);
RLYellow.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
200));
RLYellow.setBackgroundColor(Color.YELLOW);
btnClick.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "click", Toast.LENGTH_SHORT).show();
RLGreen.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PA开发者_Python百科RENT));
}
});
RLChange.addView(RLGreen);
RLChange.addView(RLYellow);
setContentView(RLChange);
Log Cat
03-10 11:01:35.328: INFO/NotificationService(574): enqueueToast pkg=Changewidth.com callback=android.app.ITransientNotification$Stub$Proxy@43948648 duration=0
03-10 11:01:35.388: DEBUG/AndroidRuntime(1505): Shutting down VM
03-10 11:01:35.388: WARN/dalvikvm(1505): threadid=3: thread exiting with uncaught exception (group=0x4001aa28)
03-10 11:01:35.398: ERROR/AndroidRuntime(1505): Uncaught handler: thread main exiting due to uncaught exception
03-10 11:01:35.449: ERROR/AndroidRuntime(1505): java.lang.ClassCastException: android.view.ViewGroup$LayoutParams
03-10 11:01:35.449: ERROR/AndroidRuntime(1505): at android.widget.LinearLayout.measureVertical(LinearLayout.java:326)
03-10 11:01:35.449: ERROR/AndroidRuntime(1505): at android.widget.LinearLayout.onMeasure(LinearLayout.java:278)
03-10 11:01:35.449: ERROR/AndroidRuntime(1505): at android.view.View.measure(View.java:7703)
03-10 11:01:35.449: ERROR/AndroidRuntime(1505): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:2989)
03-10 11:01:35.449: ERROR/AndroidRuntime(1505): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
03-10 11:01:35.449: ERROR/AndroidRuntime(1505): at android.view.View.measure(View.java:7703)
03-10 11:01:35.449: ERROR/AndroidRuntime(1505): at android.widget.LinearLayout.measureVertical(LinearLayout.java:464)
03-10 11:01:35.449: ERROR/AndroidRuntime(1505): at android.widget.LinearLayout.onMeasure(LinearLayout.java:278)
03-10 11:01:35.449: ERROR/AndroidRuntime(1505): at android.view.View.measure(View.java:7703)
03-10 11:01:35.449: ERROR/AndroidRuntime(1505): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:2989)
03-10 11:01:35.449: ERROR/AndroidRuntime(1505): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
03-10 11:01:35.449: ERROR/AndroidRuntime(1505): at android.view.View.measure(View.java:7703)
03-10 11:01:35.449: ERROR/AndroidRuntime(1505): at android.view.ViewRoot.performTraversals(ViewRoot.java:747)
03-10 11:01:35.449: ERROR/AndroidRuntime(1505): at android.view.ViewRoot.handleMessage(ViewRoot.java:1613)
03-10 11:01:35.449: ERROR/AndroidRuntime(1505): at android.os.Handler.dispatchMessage(Handler.java:99)
03-10 11:01:35.449: ERROR/AndroidRuntime(1505): at android.os.Looper.loop(Looper.java:123)
03-10 11:01:35.449: ERROR/AndroidRuntime(1505): at android.app.ActivityThread.main(ActivityThread.java:4203)
03-10 11:01:35.449: ERROR/AndroidRuntime(1505): at java.lang.reflect.Method.invokeNative(Native Method)
03-10 11:01:35.449: ERROR/AndroidRuntime(1505): at java.lang.reflect.Method.invoke(Method.java:521)
03-10 11:01:35.449: ERROR/AndroidRuntime(1505): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
03-10 11:01:35.449: ERROR/AndroidRuntime(1505): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
03-10 11:01:35.449: ERROR/AndroidRuntime(1505): at dalvik.system.NativeStart.main(Native Method)
03-10 11:01:35.479: INFO/Process(574): Sending signal. PID: 1505 SIG: 3
03-10 11:01:35.479: INFO/dalvikvm(1505): threadid=7: reacting to signal 3
03-10 11:01:35.619: INFO/dalvikvm(1505): Wrote stack trace to '/data/anr/traces.txt'
03-10 11:01:35.779: DEBUG/dalvikvm(623): GC freed 3557 objects / 196272 bytes in 448ms
In onClick Listener add this code.
Button button = (Button) findViewById(view.getId());
LinearLayout rLGreen = ((LinearLayout) button.getParent());
rLGreen.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
It will work.
Hi you can change the width and height of your layout using this code.
RelativeLayout.LayoutParams parms = new RelativeLayout.LayoutParams(width,height);
RelativeLayout layout1 = (RelativeLayout) findViewById(R.id.layout1);
lauout1.setLayoutParams(parms);
These supply parameters to the parent of this view specifying how it should be arranged. There are many subclasses of ViewGroup.LayoutParams
and these correspond to the different subclasses of ViewGroup
that are responsible for arranging their children.
So basically, if you are adding a view to another, you MUST set the LayoutParams
of the view to the LayoutParams
type that the parent uses or you will get a runtime error.
Are you using the correct LayoutParams class for your particular view? Try using LinearLayout.LayoutParams.
Remember that the LayoutParams are use by the Parent(viewgroup of parent) in order to determine how to layout this View.
Thus for example if you have the following
LinearLayout
RelativeLayout1
RelativeLayout2
And you want to change the width or height of the RelativeLayout1 or RelativeLayout2, then you need to do something like this.
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 200);
//Get handle to RelativeLayout1 assuming it is R1, you would call
R1.setLayoutParams(lp);
You would need to do the same to change RelativeLayout2's LayoutParams also.
Is is easy to forget the hierarchy, and to assume that you need to set the layout of the child, but it is the parent that determines what attributes are supported (eg. Alignment, or gravity etc..)
Hope this helps. It has burned me a few times....
精彩评论