簡體   English   中英

Android:以編程方式更改布局的根視圖高度

[英]Android: Changing the height of root view of a layout programmatically

我是Android的新手。 任何人都可以告訴我如何在Android中以編程方式更改布局的RootView的高度? 我試過的代碼如下

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

            View view = inflater.inflate(R.layout.sample, null);
            view.getRootView().getLayoutParams().height = value;

執行此操作后,我在上面的行中得到一個NullPointerException 誰能幫我? 提前致謝。

inflate調用返回的視圖是根視圖,因此這應該有效:

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

如果您要提供ViewGroup以像這樣膨脹:

View view = inflater.inflate(R.layout.sample, containerForTheInflatedView);

然后view將是ViewGroup,而不是您想要的根視圖。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM