Android:以编程方式更改布局的根视图高度

akh*_*akh 3 android android-layout android-linearlayout

我是Android的新手.任何人都可以告诉我如何RootView在Android中以编程方式更改布局的高度?我试过的代码如下

LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            View view = inflater.inflate(R.layout.sample, null);
            view.getRootView().getLayoutParams().height = value;
Run Code Online (Sandbox Code Playgroud)

执行此操作后,我NullPointerException在上面的行中获得了一个.谁能帮我?提前致谢.

nmw*_*nmw 5

inflate调用返回的视图是根视图,因此这应该有效:

View view = inflater.inflate(R.layout.sample, null);
view.setLayoutParams(new ListView.LayoutParams(LayoutParams.MATCH_PARENT,<height>));
Run Code Online (Sandbox Code Playgroud)

如果您要提供ViewGroup以像这样膨胀:

View view = inflater.inflate(R.layout.sample, containerForTheInflatedView);
Run Code Online (Sandbox Code Playgroud)

然后view是ViewGroup,而不是您想要的根视图.