如何从代码中更改LinearLayout的大小

时间:2014-05-21 11:41:35

标签: android android-layout

我有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)
...

如何解决?

2 个答案:

答案 0 :(得分:3)

试试这个,

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

答案 1 :(得分:0)

将ViewGroup替换为RelativLayout

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