Android - 以编程方式更改膨胀的布局高度

时间:2016-01-08 07:45:42

标签: android android-layout

我目前正致力于以编程方式更改布局的高度。

这是布局初始化的片段:

setContentView(R.layout.activity_main);


        LayoutInflater  inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View pagerItemInflater = (View) inflater.inflate(R.layout.activity_main, null);

        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        LayoutResize layoutResize = new LayoutResize();

RelativeLayout relativeViewPager =(RelativeLayout) findViewById(R.id.relativeViewPager);
        int relativeViewPagerHeight = layoutResize.height(75, displayMetrics);
        ViewGroup.LayoutParams viewPagerParams = relativeViewPager.getLayoutParams();
        viewPagerParams.height = relativeViewPagerHeight;
        relativeViewPager.setLayoutParams(viewPagerParams);

        RelativeLayout viewPagerItem =(RelativeLayout) pagerItemInflater.findViewById(R.id.viewPagerItem);
        int viewPagerItemWidth = layoutResize.width(100, displayMetrics);
        int viewPagerItemHeight = layoutResize.height(28, displayMetrics);
        RelativeLayout.LayoutParams viewPagerItemParams = new RelativeLayout.LayoutParams(viewPagerItemWidth, viewPagerItemHeight);
        //ViewGroup.LayoutParams viewPagerItemParams = viewPagerItem.getLayoutParams();
        viewPagerItemParams.height = viewPagerItemHeight;
        viewPagerItem.setLayoutParams(viewPagerItemParams);

问题是,我无法获得第二个viewPagerItem调整大小到屏幕的28%。当我使用log时,viewPagerItemHeight显示我想要的适当高度,但布局没有按预期调整大小。

我已经阅读过,有些人说View pagerItemInflater = (View) inflater.inflate(R.layout.activity_main, null); null应该是根类吗? R.layout.activity_main?但我不能在那里使用它,它说cannot resolve method inflate(int,int)

0 个答案:

没有答案