問題描述
佈局僅在屏幕關閉或離開應用程序並返回後顯示兒童 (Layout only shows children after screen turn off or leaving the app and coming back)
I am having lots of troubles with this. It is working properly on my Nexus 7 tablet, but not on my Samsung S3.
I am adding views to a LinearLayout called ItemsBar, but nothing shows up inside it until I press the screen off button and then screen on, or if I leave the app by switching to another app and back to it. Showing the keyboard also shows the views.
//Load Images into ItemBar
for (int currentItemImage : ItemIDs){
ImageView i = new ImageView(this);
i.setImageResource(currentItemImage);
i.setTag(currentItemImage);
i.setClickable(true);i.setPadding(4, 0, 4, 0);
i.setOnClickListener(ItemOnClick);
i.setLayoutParams(ItemLP);
ItemsBar.addView(i);
}
This is not done at OnCreate. It is done on button click. The OnStart and OnResume are unused.
By inspecting, the layout does have 20 children.
Note: to prove that there is nothing wrong with this code, resources, and Params, they actually do show up after screen turn off or switching app. And this works perfectly on the Nexus 7.
I tried using invalidate() to children and to the layout, to no avail.
What can I do?
參考解法
方法 1:
Try to call ItemsBar.requestLayout()
after all items were added.
方法 2:
The error was in OnLayoutChangeListener(). It seems that Nexus 7 with Android 4.3 dealt differently (maybe requested it differently) than my Samsung S3 with Android 4.1.2. I had to remove it completely.
(by zed、sergej shafarenka、zed)