gpt4 book ai didi

android - 是否可以在运行时更改 Android 中的布局宽度和高度?

转载 作者:IT王子 更新时间:2023-10-28 23:35:43 25 4
gpt4 key购买 nike

我有两个相对的布局。两者都具有填充父级的宽度和 100 倾角的高度。我的要求是当我单击第一个布局时,它应该缩小到 50dip 高度,而另一个扩展到 150dip 高度。这样做时我遇到了异常。请帮助我如何在运行时更改布局宽度和高度。

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

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

最佳答案

在 onClick Listener 中添加此代码。

Button button = (Button) findViewById(view.getId());
LinearLayout rLGreen = ((LinearLayout) button.getParent());
rLGreen.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

它会起作用的。

关于android - 是否可以在运行时更改 Android 中的布局宽度和高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5151056/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com