简体   繁体   English

以编程方式将width_fill_parent设置为Linearlayout

[英]Set width to fill_parent to Linearlayout programmatically

I am using slider panel in my application. 我在应用程序中使用滑块面板。

Now, my problem is that when I rotate device screen to portrait, Its width does not change. 现在,我的问题是,当我将设备屏幕旋转到纵向时,其宽度不会改变。 I tried changing some code and Its width was being perfect but was also getting error. 我尝试更改一些代码,它的宽度很完美,但也出现错误。 so please check this. 所以请检查一下

My code is as follow. 我的代码如下。 slidingPanel is LinearLayout so need to set width to it. slidePanel是LinearLayout,因此需要为其设置宽度。

Code: 码:

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

FILL_PARENT is represented by the static final int ViewGroup.FILL_PARENT . FILL_PARENT由静态最终int ViewGroup.FILL_PARENT表示 But note that since API version 8, FILL_PARENT has been and replaced by MATCH_PARENT. 但请注意,自API版本8起,FILL_PARENT已被MATCH_PARENT取代。 So use that in your LinearLayout.LayoutParams objects to set FILL_PARENT programatically. 因此,请在LinearLayout.LayoutParams对象中使用它来以编程方式设置FILL_PARENT。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM