gpt4 book ai didi

android - 如何从代码更改大小 LinearLayout

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:25:16 24 4
gpt4 key购买 nike

我有 LinearLayot 和 LinearLayout,以及 TableLayout 和 TableRow。当我单击按钮时,A 会更改大小。当我这样做时:

    @Override
public void onClick(View v) {
private LinearLayout commonCardContainer = (LinearLayout) findViewById(R.id.commonCardContainer);
ViewGroup.LayoutParams params = commonCardContainer.getLayoutParams();
params.width = 50;
params.height = 50;
commonCardContainer.setLayoutParams(new ViewGroup.LayoutParams(params));
}

我有错误:

 05-21 15:22:31.084: E/AndroidRuntime(9272): FATAL EXCEPTION: main
05-21 15:22:31.084: E/AndroidRuntime(9272): java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
05-21 15:22:31.084: E/AndroidRuntime(9272): at android.widget.RelativeLayout$DependencyGraph.findRoots(RelativeLayout.java:1317)
05-21 15:22:31.084: E/AndroidRuntime(9272): at android.widget.RelativeLayout$DependencyGraph.getSortedViews(RelativeLayout.java:1264)
05-21 15:22:31.084: E/AndroidRuntime(9272): at android.widget.RelativeLayout.sortChildren(RelativeLayout.java:292)
05-21 15:22:31.084: E/AndroidRuntime(9272): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:314)
05-21 15:22:31.084: E/AndroidRuntime(9272): at android.view.View.measure(View.java:12948)
05-21 15:22:31.084: E/AndroidRuntime(9272): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5005)
05-21 15:22:31.084: E/AndroidRuntime(9272): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
05-21 15:22:31.084: E/AndroidRuntime(9272): at android.view.View.measure(View.java:12948)
05-21 15:22:31.084: E/AndroidRuntime(9272): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5005)
05-21 15:22:31.084: E/AndroidRuntime(9272): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
05-21 15:22:31.084: E/AndroidRuntime(9272): at android.widget.LinearLayout.measureVertical(LinearLayout.java:660)
05-21 15:22:31.084: E/AndroidRuntime(9272): at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
05-21 15:22:31.084: E/AndroidRuntime(9272): at android.view.View.measure(View.java:12948)
05-21 15:22:31.084: E/AndroidRuntime(9272): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5005)
05-21 15:22:31.084: E/AndroidRuntime(9272): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
...

如何解决?

最佳答案

试试这个,

@Override
public void onClick(View v) {
private LinearLayout commonCardContainer = (LinearLayout) findViewById(R.id.commonCardContainer);
LinearLayout.LayoutParams params = commonCardContainer.getLayoutParams();
params.width = 50;
params.height = 50;
commonCardContainer.setLayoutParams(params);
}

关于android - 如何从代码更改大小 LinearLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23782175/

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