Android – how to change the size of LinearLayout from code
•
Android
I have linearlayot and LinearLayout, tablelayout and tablerow. When I click the button, a changes the size. When I do this:
@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));
}
I made a mistake:
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)
...
How to solve it?
resolvent:
Try this,
@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);
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码