Why is my image disappearing in landscape mode?

 ̄綄美尐妖づ 提交于 2019-12-23 22:45:03

问题


I have an image in portrait mode, which is displayed correctly. When I change the configuration to landscape, the image is not displayed.

Note that the LinearLayout, enclosing the ImageView is displayed right(I checked it by changing the background). But when I changed the background for the ImageView, the background was also not shown.

   <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10dp" android:layout_weight="3">
           <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_vertical" android:minHeight="48dp" android:minWidth="48dp" android:src="@drawable/xyz" android:id="@+id/img"/>


回答1:


OK, your layout file is unnecessarily large and complex. So let's try a couple of things-

First, try creating a simple version of your XML file that contains the barebones for displaying the image. If that works in both portrait and landscape, you know that the problem is not your activity or your image.

If the first step works, the next step would be to work on simplifying your layout file, there is a lot that could be pruned. It goes seven layers deep at points, that should almost never happen. Here is a good blog post by an Android engineer on better layout design. You can find similar resources elsewhere.




回答2:


Do you have different files specified for portrait and landscape more? I.e., do you have a layout file with the same name in the layout folder as well as the layout-land folder? If so, make sure both files include the ImageView.




回答3:


You are most likely specifying the file you want to display like

myImageView.setImage("myfile.jpg");

in your onCreate method.

Move this code to onResume (create onResume with the @Override if you do not already have it) This will fix your problem



来源:https://stackoverflow.com/questions/6497064/why-is-my-image-disappearing-in-landscape-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!