Android 方向改变导致东西消失
下面是我在 SimpleCursorAdapter 中的行模板的布局。
id 为 next_arrow 的 ImageView 在方向更改时永久消失(返回到原始方向不会使其重新出现)。如果我用 @drawable/icon 替换 @drawable/arrow (标准 sym_def_app_icon 的副本),它可以正常工作。所有相关的可绘制对象都是对 PNG 文件的简单引用。
我认为这可能与不同尺寸的图标有关,但我将 @drawable/icon 的大小调整为与箭头完全相同的尺寸,并且它仍然工作正常,而箭头则不然。
更新
好吧,事情变得更奇怪了。我将 arrow.png 重命名为 something_not_used_arrow.png 并且...它有效。到底是怎么回事?
这不是我第一次看到这种情况发生。今天早些时候,我有另一个可绘制的 PNG,它在 Eclipse 图形布局视图中显示良好,但在我重命名该文件之前根本拒绝在模拟器或设备上显示。我尝试清理和刷新项目,然后重新启动 Eclipse。
另一个更新
刚刚在不同的情况下再次发生了这种情况。在 Activity onCreate() 中,我对一些资源调用 findViewById() 来配置它们,第一次加载时一切正常。当由于方向更改而调用 onCreate() 时,findViewById() 对所有这些资源返回 null。这太令人抓狂了,涉及 android:configChanges="keyboardHidden|orientation" 的答案根本不是真正的解决方案,它们只是掩盖了问题。
在本例中,我通过将活动的布局 xml 从 share.xml 重命名为 share_activity_layout.xml 来“修复”它。我开始认为 R 文件生成和资源 ID 管理总体上存在某种错误。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="90dp"
android:background="@drawable/product_list_divider"
android:paddingTop="10dp">
<ImageView android:src="@drawable/ninja_20_65"
android:id="@+id/category_icon"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_marginTop="3dp"/>
<LinearLayout android:orientation="vertical"
android:layout_weight="5"
android:layout_width="0dp"
android:layout_height="wrap_content">
<TextView android:id="@+id/category_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Name"
style="@style/CategoryHeading" android:layout_marginTop="5dp"/>
<TextView android:id="@+id/category_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Description"
style="@style/CategoryDescription"/>
</LinearLayout>
<ImageView android:src="@drawable/arrow"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_marginRight="10dp"
android:layout_height="wrap_content" android:layout_marginTop="20dp" android:id="@+id/next_arrow"/>
</LinearLayout>
我使用的是安卓2.1。
Below is my layout for the row template in a SimpleCursorAdapter.
The ImageView with id next_arrow disappears permanently on orientation change (going back to original orientation doesn't make it re-appear). If I replace @drawable/arrow with @drawable/icon (copy of the standard sym_def_app_icon) it works fine. All the drawables in question are simple references to PNG files.
I thought it might be somehow related to the icons being of different dimensions, but I resized @drawable/icon to be the exact same dimensions as the arrow and it still worked fine while the arrow didn't.
Update
Okay it gets even weirder. I renamed the arrow.png to something_not_called_arrow.png and... it works. What is going on?
This isn't the first time I've seen this happen. Earlier today I had another drawable PNG that showed fine in the Eclipse Graphical Layout view, but refused to show up at all in the Emulator or on a device until I renamed the file. I tried cleaning and refreshing the project, and restarting Eclipse.
Another Update
Just had this happen again in a different situation. In Activity onCreate() I call findViewById() on a few resources to configure them, everything works fine on first load. When onCreate() is called due to an orientation change, findViewById() returns null for all these resources. This is maddening, and the answers that involve android:configChanges="keyboardHidden|orientation"
aren't real solutions at all, they are just masking the problem.
In this case I "fixed" it by renaming the layout xml for the activity from share.xml to share_activity_layout.xml. I'm starting to think there is some kind of bug in the R file generation and resource ID management in general.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="90dp"
android:background="@drawable/product_list_divider"
android:paddingTop="10dp">
<ImageView android:src="@drawable/ninja_20_65"
android:id="@+id/category_icon"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_marginTop="3dp"/>
<LinearLayout android:orientation="vertical"
android:layout_weight="5"
android:layout_width="0dp"
android:layout_height="wrap_content">
<TextView android:id="@+id/category_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Name"
style="@style/CategoryHeading" android:layout_marginTop="5dp"/>
<TextView android:id="@+id/category_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Description"
style="@style/CategoryDescription"/>
</LinearLayout>
<ImageView android:src="@drawable/arrow"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_marginRight="10dp"
android:layout_height="wrap_content" android:layout_marginTop="20dp" android:id="@+id/next_arrow"/>
</LinearLayout>
I'm using Android 2.1.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的资源文件夹中有许多子目录。
等等..
如果您使用R.drawable.arrow ,android会寻找drawable来加载它。问题是(实际上这是一个功能),如果您处于横向模式,它将首先在可绘制区域中查找,如果在那里找不到任何内容,则仅在可绘制区域之后查找。
这同样适用于不同的分辨率等。
所以我的猜测是你的可绘制区域搞砸了,我鼓励你看看那里是否有任何文件。如果有,请删除它们。
这同样适用于布局。
Your resource folder is has many sub-directories in it.
and so on..
if you use R.drawable.arrow, android will look into drawable to load it. The problem is (and actually it's a feature), that if you're in landscape mode it will look first in drawable-land and, if it can't find anything there, only after in drawable.
The same applies to different resolutions, etc.
So my guess is you have drawable-land messed up and I encourage you to give a look there to see if there is any file. If so, delete them.
Same applies for the layouts.
我将
arrow.png
重命名为something_not_used_arrow.png
并且它可以工作。随后出现了一个类似的问题,我通过将活动的布局 xml 从 share.xml 重命名为 share_activity_layout.xml 来“修复”。
我认为 R 文件生成和资源 ID 管理总体上存在错误。
I renamed the
arrow.png
tosomething_not_called_arrow.png
and it works.There was a subsequent similar issue which I "fixed" by renaming the layout xml for the activity from share.xml to share_activity_layout.xml.
I think there is a bug in the R file generation and resource ID management in general.
两个想法。
您的资源文件夹中是否还有另一个名为 arrow 的可绘制对象?当方向发生变化时,android 将重新创建 Activity 并重新加载布局和图像。它可能从某个地方拾取了不同的图像。这些可以解释为什么当您重命名可绘制对象时它会起作用。
第二。为什么将图像宽度设置为0?将其设置为换行内容可能就可以了。
Two thoughts.
is there another drawable in your resource folder called arrow? when the orientation change occurs android will recreate the activity and reload the layout and images. it may be picking up a different image from somewhere. those could explain why it works when you rename the drawable.
second. why did you set the image width to 0? it will probably be ok to set it to wrap content.