以编程方式将width_parent的宽度设置为Linearlayout

时间:2013-06-08 10:48:43

标签: android android-framelayout

我在我的应用程序中使用滑块面板。

现在,我的问题是当我将设备屏幕旋转为肖像时,它的宽度不会改变。我尝试更改一些代码,它的宽度是完美的,但也有错误。所以请检查一下。

我的代码如下。 slidingPanel是LinearLayout,因此需要设置宽度。

代码:

metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
panelWidth = (int) ((metrics.widthPixels) * 0.70);

headerPanel = (RelativeLayout) findViewById(R.id.header);
headerPanelParameters = (LinearLayout.LayoutParams) headerPanel
            .getLayoutParams();
headerPanelParameters.width = metrics.widthPixels;
headerPanel.setLayoutParams(headerPanelParameters);

menuPanel = (RelativeLayout) findViewById(R.id.menuPanel);
menuPanelParameters = (FrameLayout.LayoutParams) menuPanel
            .getLayoutParams();
menuPanelParameters.width = panelWidth;
menuPanel.setLayoutParams(menuPanelParameters);

slidingPanel = (LinearLayout) findViewById(R.id.slidingPanel);
slidingPanelParameters = (FrameLayout.LayoutParams) slidingPanel
            .getLayoutParams();
slidingPanelParameters.width = metrics.widthPixels;
slidingPanel.setLayoutParams(slidingPanelParameters);

1 个答案:

答案 0 :(得分:0)

FILL_PARENT由静态final int ViewGroup.FILL_PARENT表示。但请注意,自API版本8起,FILL_PARENT已被MATCH_PARENT取代。因此,在LinearLayout.LayoutParams对象中使用它以编程方式设置FILL_PARENT。