简体   繁体   中英

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.

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 . But note that since API version 8, FILL_PARENT has been and replaced by MATCH_PARENT. So use that in your LinearLayout.LayoutParams objects to set FILL_PARENT programatically.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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